PyLint“无法导入"错误 - 如何设置 PYTHONPATH? [英] PyLint "Unable to import" error - how to set PYTHONPATH?
问题描述
我在 Windows 上的 Wing IDE 中运行 PyLint.我的项目中有一个子目录(包),在包内我从顶层导入一个模块,即.
I'm running PyLint from inside Wing IDE on Windows. I have a sub-directory (package) in my project and inside the package I import a module from the top level, ie.
__init__.py
myapp.py
one.py
subdir\
__init__.py
two.py
Inside two.py
我有 import one
并且这在运行时工作正常,因为顶级目录(其中 myapp.py
> 正在运行)位于 Python 路径中.但是,当我在 two.py 上运行 PyLint 时,它给了我一个错误:
Inside two.py
I have import one
and this works fine at runtime, because the top-level directory (from which myapp.py
is run) is in the Python path. However, when I run PyLint on two.py it gives me an error:
F0401: Unable to import 'one'
我该如何解决这个问题?
How do I fix this?
推荐答案
我知道有两个选项.
一,更改 PYTHONPATH
环境变量以包含模块上方的目录.
One, change the PYTHONPATH
environment variable to include the directory above your module.
或者,编辑 ~/.pylintrc
以包含模块上方的目录,如下所示:
Alternatively, edit ~/.pylintrc
to include the directory above your module, like this:
[MASTER]
init-hook='import sys; sys.path.append("/path/to/root")'
(或者在其他版本的 pylint 中,init-hook 要求您将 [General] 更改为 [MASTER])
(Or in other version of pylint, the init-hook requires you to change [General] to [MASTER])
这两个选项都应该有效.
Both of these options ought to work.
希望有所帮助.
这篇关于PyLint“无法导入"错误 - 如何设置 PYTHONPATH?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!