PyLint“无法导入”错误 - 如何设置 PYTHONPATH?
我在 Windows 上从 Wing IDE 内部运行 PyLint。我的项目中有一个子目录(包),在包内我从顶层导入一个模块,即。
__init__.py
myapp.py
one.py
subdir\
__init__.py
two.py
在 two.py
内部,我有 import one
并且这在运行时工作得很好,因为顶级目录(从中运行 myapp.py
) 位于 Python 路径中。但是,当我在 Two.py 上运行 PyLint 时,它给我一个错误:
F0401: Unable to import 'one'
如何解决这个问题?
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
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
我遇到了同样的问题,由于我找不到答案,我希望这可以帮助任何有类似问题的人。
我使用 Flymake 和 epylint。基本上我所做的是添加一个 dired-mode-hook 来检查 dired 目录是否是 python 包目录。如果是,我将其添加到 PYTHONPATH 中。就我而言,如果一个目录包含名为“setup.py”的文件,我认为该目录是一个 python 包。
希望这有帮助。
I had the same problem and since i could not find a answer I hope this can help anyone with a similar problem.
I use flymake with epylint. Basically what i did was add a dired-mode-hook that check if the dired directory is a python package directory. If it is I add it to the PYTHONPATH. In my case I consider a directory to be a python package if it contains a file named "setup.py".
Hope this helps.
关键是将项目目录添加到 sys.path 中,而不考虑 env 变量。
对于使用 VSCode 的人来说,如果您的项目有基目录,这里有一个单行解决方案:
让我解释一下:
re.search(r".+\/" + base_dir , os.getcwd()).group()
:根据编辑文件查找基目录os.path.join(os.getcwd(), base_dir)
:添加cwd 到
sys.path
以满足命令行环境仅供参考,这是我的 .pylintrc:
https://gist.github.com/chuyik/f0ffc41a6948b6c87c7160151ffe8c2f
The key is to add your project directory to
sys.path
without considering about the env variable.For someone who use VSCode, here's a one-line solution for you if there's a base directory of your project:
Let me explain it a little bit:
re.search(r".+\/" + base_dir, os.getcwd()).group()
: find base directory according to the editing fileos.path.join(os.getcwd(), base_dir)
: addcwd
tosys.path
to meet command line environmentFYI, here's my .pylintrc:
https://gist.github.com/chuyik/f0ffc41a6948b6c87c7160151ffe8c2f
我遇到了同样的问题,并通过在 virtualenv 中安装 pylint 来修复它,然后将 .pylintrc 文件添加到我的项目目录中,文件中包含以下内容:
I had this same issue and fixed it by installing pylint in my virtualenv and then adding a .pylintrc file to my project directory with the following in the file:
安装Python时,可以设置路径。如果路径已经定义,那么您可以在 VS Code 中执行以下操作:按 Ctrl+Shift+P 并输入 Python:选择 Interpreter 并选择更新版本的 Python。请点击此链接了解更多信息,https://code.visualstudio.com/docs/python/environments
When you install Python, you can set up the path. If path is already defined then what you can do is within VS Code, hit Ctrl+Shift+P and type Python: Select Interpreter and select updated version of Python. Follow this link for more information, https://code.visualstudio.com/docs/python/environments
首先,转到您的 VS Code,然后按“ctrl + shift + p”
然后搜索 settings.json
然后将以下代码粘贴到 settings.jason 中。我希望问题能够得到解决。
{
}
First, go to your VS Code then press "ctrl + shift + p"
Then search settings.json
Then paste the below code inside the settings.jason.I hope the problem will be solved.
{
}
我在尝试提交 PR 时遇到此错误。我最终要做的只是在发生“导入”的同一行添加#pylint:disable=E0401。
这有助于我通过自动测试。
I got this error when trying to submit a PR. What I end up doing is just to add #pylint: disable=E0401 on the same line where "import" happens.
This helps me to pass the auto test.
这一切对我有用!
.pylintrc
this all work for me!
.pylintrc
我刚刚发现的一种解决方法是实际上只为整个包运行 PyLint,而不是单个文件。不知何故,它设法找到导入的模块。
One workaround that I only just discovered is to actually just run PyLint for the entire package, rather than a single file. Somehow, it manages to find imported module then.
这是一个老问题,但没有公认的答案,所以我建议这样做:将two.py中的导入语句更改为:
在我当前的环境(Python 3.6,VSCode使用pylint 2.3.1)中,这会清除标记的语句。
This is an old question but has no accepted answer, so I'll suggest this: change the import statement in two.py to read:
In my current environment (Python 3.6, VSCode using pylint 2.3.1) this clears the flagged statement.
我发现这在具有 pipelinenv 虚拟环境的本地 .pylintrc 文件中运行良好:
请参阅 这篇文章了解有关站点包的信息。
I found this worked nicely in my local
.pylintrc
file with a pipenv virtual environment:See this post for info about site packages.
我遇到了同样的问题并找到了不同的解决方案。这里已经提出的一些解决方案对我来说不起作用或不合需要。
pylint
版本3.0.3
并且find_pylintrc
不再存在。init-root
中使用绝对路径。我的解决方案是将以下代码段添加到
pylintrc
文件的MAIN
部分。 (如果您使用不同版本的pylint
,您的内容可能与MAIN
不同)。源根目录列表可以是绝对的,也可以是相对于调用
pylint
的工作文件夹,或者是项目根文件夹(如果从 IDE 中调用pylint
)。I ran into the same issue and found a different solution. Some of the solutions already presented here didn't work or were not desirable for me.
pylint
version3.0.3
andfind_pylintrc
is not present anymore.init-root
.The solution in my case was to add the following snippet to the
MAIN
section of thepylintrc
file. (You may have something different thanMAIN
if you are using a different version ofpylint
).The list of source roots are either absolute or relative to the working folder from where
pylint
is being called, or the project root folder in casepylint
is called from within the IDE.如果有人正在寻找一种方法来将 pylint 作为 PyCharm 中的外部工具运行并使其与他们的虚拟环境一起工作(为什么我提出这个问题),以下是我解决它的方法:
$PyInterpreterDirectory$/pylint
--rcfile =$ProjectFileDir$/pylintrc -rn $FileDir$
$FileDir$
现在使用 pylint 作为外部工具将在您使用通用配置选择的任何目录上运行 pylint文件并使用为您的项目配置的任何解释器(可能是您的 virtualenv 解释器)。
In case anybody is looking for a way to run pylint as an external tool in PyCharm and have it work with their virtual environments (why I came to this question), here's how I solved it:
$PyInterpreterDirectory$/pylint
--rcfile=$ProjectFileDir$/pylintrc -r n $FileDir$
$FileDir$
Now using pylint as an external tool will run pylint on whatever directory you have selected using a common config file and use whatever interpreter is configured for your project (which presumably is your virtualenv interpreter).
Mac 用户:如果您使用 Anaconda 3 和 vsCode 并拥有多个环境,则通过
settings.json
指向 vsCode 的以下路径也可以:Mac user: If you're using Anaconda 3 w/ vsCode and have multiple environments, pointing to the following path via
settings.json
for vsCode works as well:如果您使用的是 Windows:
Z:\YourProjectFolder\Scripts\python.exe
settings.json
"python.pythonPath": "Z:\\YourProjectFolder\\Scripts\\python.exe"
If you are using Windows:
Z:\YourProjectFolder\Scripts\python.exe
settings.json
"python.pythonPath": "Z:\\YourProjectFolder\\Scripts\\python.exe"
您好,我能够从不同的目录导入包。我刚刚做了以下事情:
注意:我正在使用 VScode
创建 Python 包的步骤
使用 Python 包非常简单。您需要做的就是:
例如:您有一个名为 Framework 的文件夹,其中保存所有自定义类,您的工作就是在名为的文件夹中创建一个 __init__.py 文件框架。
在导入时,您需要以这种方式导入--->
这样E0401错误就消失了
Framework 是您刚刚创建 __init__.py 的文件夹,
base 是您的自定义模块,您需要导入并使用它
希望有帮助!!!!
Hello i was able to import the packages from different directory. I just did the following:
Note: I am using VScode
Steps to Create a Python Package
Working with Python packages is really simple. All you need to do is:
For example: you have a folder called Framework where you are keeping all the custom classes there and your job is to just create a __init__.py file inside the folder named Framework.
And while importing you need to import in this fashion--->
so the E0401 error disappears
Framework is the folder where you just created __init__.py and
base is your custom module which you are required to import into and work upon
Hope it helps!!!!
对于 pylint >=3.0,
find_pylintrc
已被删除,您需要执行以下操作:这使用新的
find_default_config_files
函数。For pylint >=3.0 the
find_pylintrc
has been removed, instead you'll need to do something like:This uses the new
find_default_config_files
function.也许通过手动将目录附加到 PYTHONPATH 中?
Maybe by manually appending the dir inside the PYTHONPATH?
如果您使用 vscode,请确保您的包目录位于 _pychache__ 目录之外。
if you using vscode,make sure your package directory is out of the _pychache__ directory.
我知道有两种选择。
一,更改 PYTHONPATH 环境变量以包含模块上方的目录。
,编辑
~/.pylintrc
以包含模块上方的目录,如下所示:(或者在 pylint 的其他版本中,init-hook 要求您将 [General] 更改为 [MASTER])
或者 这些选项应该有效。
There are two options I'm aware of.
One, change the
PYTHONPATH
environment variable to include the directory above your module.Alternatively, edit
~/.pylintrc
to include the directory above your module, like this:(Or in other version of pylint, the init-hook requires you to change [General] to [MASTER])
Both of these options ought to work.
在
init-hook
中更改路径的解决方案很好,但我不喜欢必须在其中添加绝对路径,因此我无法在项目的开发人员之间共享此 pylintrc 文件。使用 pylintrc 文件相对路径的解决方案对我来说效果更好:请注意,
pylint.config.PYLINTRC
也存在,并且与find_pylintrc()
具有相同的值。The solution to alter path in
init-hook
is good, but I dislike the fact that I had to add absolute path there, as result I can not share this pylintrc file among the developers of the project. This solution using relative path to pylintrc file works better for me:Note that
pylint.config.PYLINTRC
also exists and has the same value asfind_pylintrc()
.通过在venv下配置pylint路径可以解决该问题:
$ cat .vscode/settings.json
The problem can be solved by configuring pylint path under venv:
$ cat .vscode/settings.json
我在项目的根目录中添加了一个新文件
pylintrc
,它在 PyCharm IDE 中适用于我
I've added a new file
pylintrc
in the project's root directory withand it works for me in PyCharm IDE
创建
.pylintrc
并添加Create
.pylintrc
and add两个目录中是否都有一个空的 __init__.py 文件来让 python 知道这些目录是模块?
当您不从文件夹内运行时(即可能从 pylint 的文件夹中运行,尽管我没有使用过),基本轮廓是:
这就是 python 解释器在不引用当前目录的情况下识别模块的方式,因此如果 pylint 是从自己的绝对路径运行,它将能够以
topdir.functions_etc
或topdir.subdir.other_functions
的形式访问functions_etc.py
,只要topdir
位于PYTHONPATH
上。更新:如果问题不是
__init__.py
文件,也许只需尝试将模块复制或移动到c:\Python26\Lib\site-packages
——也就是说放置附加包的常见位置,并且肯定会在您的 pythonpath 上。如果您知道如何执行 Windows 符号链接或等效操作(我不知道!),您可以这样做。还有更多选项此处:http://docs.python.org/install/index.html< /a>,包括将 sys.path 附加到开发代码的用户级目录的选项,但实际上我通常只是象征性地将本地开发目录链接到站点包 - 复制它具有相同的效果。Do you have an empty
__init__.py
file in both directories to let python know that the dirs are modules?The basic outline when you are not running from within the folder (ie maybe from pylint's, though I haven't used that) is:
This is how the python interpreter is aware of the module without reference to the current directory, so if pylint is running from its own absolute path it will be able to access
functions_etc.py
astopdir.functions_etc
ortopdir.subdir.other_functions
, providedtopdir
is on thePYTHONPATH
.UPDATE: If the problem is not the
__init__.py
file, maybe just try copying or moving your module toc:\Python26\Lib\site-packages
-- that is a common place to put additional packages, and will definitely be on your pythonpath. If you know how to do Windows symbolic links or the equivalent (I don't!), you could do that instead. There are many more options here: http://docs.python.org/install/index.html, including the option of appending sys.path with the user-level directory of your development code, but in practice I usually just symbolically link my local development dir to site-packages - copying it over has the same effect.1) sys.path 是一个列表。
2) 问题有时是 sys.path 不是你的 virtualenv.path 并且你想在你的 virtualenv 中使用 pylint
3) 所以就像说的,使用 init-hook (注意 ' 和 " 中的pylint 的解析是严格的)
或
.. 和
1) sys.path is a list.
2) The problem is sometimes the sys.path is not your virtualenv.path and you want to use pylint in your virtualenv
3) So like said, use init-hook (pay attention in ' and " the parse of pylint is strict)
or
.. and
我不知道它如何与 WingIDE 一起使用,但是为了将 PyLint 与 Geany 一起使用,我将外部命令设置为:
其中 %f 是文件名,%d 是路径。可能对某人有用:)
I don't know how it works with WingIDE, but for using PyLint with Geany, I set my external command to:
where %f is the filename, and %d is the path. Might be useful for someone :)
我必须更新系统
PYTHONPATH
变量才能添加我的 App Engine 路径。就我而言,我只需编辑~/.bashrc
文件并添加以下行:export PYTHONPATH=$PYTHONPATH:/path/to/google_appengine_folder
事实上,我首先尝试设置
init-hook
但这并没有在我的代码库中一致地解决问题(不知道为什么)。一旦我将它添加到系统路径(通常可能是个好主意),我的问题就消失了。I had to update the system
PYTHONPATH
variable to add my App Engine path. In my case I just had to edit my~/.bashrc
file and add the following line:export PYTHONPATH=$PYTHONPATH:/path/to/google_appengine_folder
In fact, I tried setting the
init-hook
first but this did not resolve the issue consistently across my code base (not sure why). Once I added it to the system path (probably a good idea in general) my issues went away.Try请注意,在 Python 3 中,
else
子句中部分的语法将是再想一想,这可能无法修复您的问题具体问题。我误解了这个问题,认为two.py 正在作为主模块运行,但事实并非如此。考虑到 Python 2.6(不从
__future__
导入absolute_import
)和 Python 3.x 处理导入方式的差异,无论如何,您都不需要为 Python 2.6 执行此操作,我不认为。尽管如此,如果您最终切换到 Python 3 并计划使用模块作为包模块和包内的独立脚本,那么保留它可能是一个好主意
东西
类似心里的
。编辑:现在为您的实际问题提供可能的解决方案。要么从包含
one
模块的目录运行 PyLint(也许通过命令行),或者在运行 PyLint 时将以下代码放在某处:基本上,作为摆弄 PYTHONPATH 的替代方法,只需确保当前工作目录是执行导入时包含
one.py
的目录。(看看 Brian 的答案,您可能可以将前面的代码分配给
init_hook
,但如果您打算这样做,那么您可以简单地附加到sys.path
他确实这么做了,这比我的解决方案稍微优雅一些。)TryNote that in Python 3, the syntax for the part in the
else
clause would beOn second thought, this probably won't fix your specific problem. I misunderstood the question and thought that two.py was being run as the main module, but that is not the case. And considering the differences in the way Python 2.6 (without importing
absolute_import
from__future__
) and Python 3.x handle imports, you wouldn't need to do this for Python 2.6 anyway, I don't think.Still, if you do eventually switch to Python 3 and plan on using a module as both a package module and as a standalone script inside the package, it may be a good idea to keep
something like
in mind.
EDIT: And now for a possible solution to your actual problem. Either run PyLint from the directory containing your
one
module (via the command line, perhaps), or put the following code somewhere when running PyLint:Basically, as an alternative to fiddling with PYTHONPATH, just make sure the current working directory is the directory containing
one.py
when you do the import.(Looking at Brian's answer, you could probably assign the previous code to
init_hook
, but if you're going to do that then you could simply do the appending tosys.path
that he does, which is slightly more elegant than my solution.)我找到了一个很好的答案。编辑你的 pylintrc 并在 master 中添加以下内容
I found a nice answer. Edit your pylintrc and add the following in master
如果您想从传递给
pylint
的当前模块/文件开始查找模块的根,这将做到这一点。如果你有一个 python 模块
~/code/mymodule/
,具有这样的顶级目录布局那么这会将
~/code/mymodule/
添加到你的 python 路径并允许 pylint 在您的 IDE 中运行,即使您在tests/test_src.py
中导入mymodule.src
也是如此。您可以将检查替换为
.pylintrc
,但是当涉及到 Python 模块的根目录时,git 目录通常是您想要的。在你提问之前
答案使用
import sys, os; sys.path.append(...)
缺少一些可以证明我的答案格式合理的东西。我通常不会以这种方式编写代码,但在这种情况下,您将不得不处理 pylintrc 配置解析器和评估器的限制。它实际上在 init_hook 回调的上下文,因此任何导入pathlib
、使用多行语句、将某些内容存储到变量等的尝试都将不起作用。我的代码的一种不太令人厌恶的形式可能如下所示:
我希望我可以使用pathlib.Path(filename).parents,它会让事情变得更容易。
If you want to walk up from the current module/file that was handed to
pylint
looking for the root of the module, this will do it.If you have a python module
~/code/mymodule/
, with a top-level directory layout like thisThen this will add
~/code/mymodule/
to your python path and allow for pylint to run in your IDE, even if you're importingmymodule.src
intests/test_src.py
.You could swap out a check for a
.pylintrc
instead but a git directory is usually what you want when it comes to the root of a python module.Before you ask
The answers using
import sys, os; sys.path.append(...)
are missing something that justifies the format of my answer. I don't normally write code that way, but in this case you're stuck dealing with the limitations of the pylintrc config parser and evaluator. It literally runsexec
in the context of the init_hook callback so any attempt to importpathlib
, use multi-line statements, store something into variables, etc., won't work.A less disgusting form of my code might look like this:
I wish I could have used
pathlib.Path(filename).parents
it would have made things much easier.