我想从 pyclbr
运行函数 readmodule
。 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 和脚本中的不同行为不满意。
最佳答案
pyclbr.readmodule
在普通模块搜索路径 sys.path
中查找模块,可选地通过 path
参数给出的附加目录进行扩充.
当您运行 python
启动交互式 session 时,sys.path
的元素之一是 '.'
,这是一个相对路径指的是当前目录,无论当前目录当时是什么。使用 os.chdir
更改当前目录会影响模块查找。
当您运行脚本时,根据您的运行方式,不同的路径可能会取代 '.'
条目,或者它可能会消失而没有替代品。在这种情况下,更改当前目录不会影响模块查找,或者会以更奇怪的方式影响它。
您应该将path
参数传递给pyclbr.readmodule
,而不是更改工作目录:
pyclbr.readmodule('car', path=['rto'])
或者,如果 rto
应该是一个包并且 car
是包的子模块,则将 'rto.car'
作为模块名称而不是 car
:
pyclbr.readmodule('rto.car')
关于python - 在另一个目录的脚本中运行时,Pyclbr readmodule 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58368949/