Pyclbr readmodule在另一个目录的脚本中运行时失败

问题描述 投票:0回答:1

我想从readmodule运行功能pyclbr。从终端调用时,Python代码在终端中运行良好,但在脚本中失败。它必须与目录更改有关。

我尝试阅读源代码,但无法推断出任何区别。

这是脚本中的代码:

import os
import pyclbr
import sys

print(os.getcwd())
os.chdir('rto')
print(os.getcwd())
source_code = pyclbr.readmodule('car')
print(source_code)
source_code = pyclbr.readmodule('transport')
print(source_code)
source_code = pyclbr.readmodule('vehicles')
print(source_code)

我使用以下命令运行上述脚本:/usr/local/bin/python3 test_readmodule.py并遇到以下错误:

/Users/aviralsrivastava/dev/generate_uml/inheritance_and_dependencies
/Users/aviralsrivastava/dev/generate_uml/inheritance_and_dependencies/rto
Inside _readmodule, module=car, path=[]
Traceback (most recent call last):
  File "test_readmodule.py", line 8, in <module>
    source_code = pyclbr.readmodule('car')
  File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pyclbr.py", line 123, in readmodule
    for key, value in _readmodule(module, path or []).items():
  File "/usr/local/Cellar/python/3.7.4_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pyclbr.py", line 190, in _readmodule
    if spec.submodule_search_locations is not None:
AttributeError: 'NoneType' object has no attribute 'submodule_search_locations'

但是,当我在Python3 Shell中运行相同的代码时,在运行脚本的python3命令所在的目录中:

➜  inheritance_and_dependencies git:(master) ✗ /usr/local/bin/python3
Python 3.7.4 (default, Sep  7 2019, 18:27:02)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import pyclbr
>>> import sys
>>>
>>> print(os.getcwd())
/Users/aviralsrivastava/dev/generate_uml/inheritance_and_dependencies
>>> os.chdir('rto')
>>> print(os.getcwd())
/Users/aviralsrivastava/dev/generate_uml/inheritance_and_dependencies/rto
>>> source_code = pyclbr.readmodule('car')
Inside _readmodule, module=car, path=[]
Inside _readmodule, module=vehicles, path=[]
Inside _readmodule, module=transport, path=[]
Inside _readmodule, module=vehicles, path=[]
returning from 157
>>> print(source_code)
{'Vehicle': <pyclbr.Class object at 0x105369510>, 'Farzi': <pyclbr.Class object at 0x1053c5e50>, 'CarPollutionPermit': <pyclbr.Class object at 0x1053c58d0>, 'BikePollutionPermit': <pyclbr.Class object at 0x1053c5e10>, 'Car': <pyclbr.Class object at 0x1052ebd50>, 'Bike': <pyclbr.Class object at 0x1053df210>}
>>> source_code = pyclbr.readmodule('transport')
Inside _readmodule, module=transport, path=[]
returning from 157
>>> print(source_code)
{'Vehicle': <pyclbr.Class object at 0x105369510>, 'Farzi': <pyclbr.Class object at 0x1053c5e50>, 'CarPollutionPermit': <pyclbr.Class object at 0x1053c58d0>, 'BikePollutionPermit': <pyclbr.Class object at 0x1053c5e10>}
>>> source_code = pyclbr.readmodule('vehicles')
Inside _readmodule, module=vehicles, path=[]
returning from 157
>>> print(source_code)
{'Vehicle': <pyclbr.Class object at 0x105369510>, 'Farzi': <pyclbr.Class object at 0x1053c5e50>}

我可以接受该错误,但不能接受shell和脚本中的不同行为。

python python-3.x cpython
1个回答
0
投票

[pyclbr.readmodule在普通模块搜索路径sys.path中寻找模块,并可选地通过path自变量给出的其他目录进行扩充。

[当您运行python以启动交互式会话时,sys.path的元素之一是'.',这是指向当前目录的相对路径,而不管当时的当前目录是什么。因此,使用os.chdir更改当前目录会影响模块查找。

运行脚本时,根据运行方式的不同,'.'条目可能会替换为其他路径,或者可能已替换掉该路径。在这种情况下,更改当前目录不会影响模块查找,也不会以奇怪的方式影响它。


而不是更改工作目录,您应该将path参数传递给pyclbr.readmodule

pyclbr.readmodule('car',  path=['rto'])

或者,如果rto应该是程序包,而car应该是程序包的子模块,则将'rto.car'作为模块名称而不是car:]]

pyclbr.readmodule('rto.car')
© www.soinside.com 2019 - 2024. All rights reserved.