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

Pyclbr readmodule fails when run in script for another directory

我想 运行 来自 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启动一个交互式会话时,sys.path的元素之一是'.',一个指向当前目录的相对路径,无论当前目录恰好在当时。使用 os.chdir 更改当前目录会影响模块查找。

当您 运行 一个脚本时,根据您 运行 它的方式,不同的路径可能会取代 '.' 条目,或者它可能会消失而没有替代品.在这种情况下,更改当前目录不会影响模块查找,或者会以更奇怪的方式影响它。


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

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

或者,如果 rto 应该是一个包并且 car 是包的子模块,则将 'rto.car' 作为模块名称而不是 car 传递:

pyclbr.readmodule('rto.car')