PyLint“无法导入”错误 - 如何设置 PYTHONPATH?

发布于 2024-08-14 08:33:54 字数 395 浏览 17 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

眼泪都笑了 2024-08-21 08:33:55

我遇到了同样的问题,由于我找不到答案,我希望这可以帮助任何有类似问题的人。

我使用 Flymake 和 epylint。基本上我所做的是添加一个 dired-mode-hook 来检查 dired 目录是否是 python 包目录。如果是,我将其添加到 PYTHONPATH 中。就我而言,如果一个目录包含名为“setup.py”的文件,我认为该目录是一个 python 包。

;;;;;;;;;;;;;;;;;
;; PYTHON PATH ;;
;;;;;;;;;;;;;;;;;

(defun python-expand-path ()
  "Append a directory to the PYTHONPATH."
  (interactive
   (let ((string (read-directory-name 
          "Python package directory: " 
          nil 
          'my-history)))
     (setenv "PYTHONPATH" (concat (expand-file-name string)
                  (getenv ":PYTHONPATH"))))))

(defun pythonpath-dired-mode-hook ()
  (let ((setup_py (concat default-directory "setup.py"))
    (directory (expand-file-name default-directory)))
    ;;   (if (file-exists-p setup_py)
    (if (is-python-package-directory directory)
    (let ((pythonpath (concat (getenv "PYTHONPATH") ":" 
                  (expand-file-name directory))))
      (setenv "PYTHONPATH" pythonpath)
      (message (concat "PYTHONPATH=" (getenv "PYTHONPATH")))))))

(defun is-python-package-directory (directory)
  (let ((setup_py (concat directory "setup.py")))
    (file-exists-p setup_py)))

(add-hook 'dired-mode-hook 'pythonpath-dired-mode-hook)

希望这有帮助。

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".

;;;;;;;;;;;;;;;;;
;; PYTHON PATH ;;
;;;;;;;;;;;;;;;;;

(defun python-expand-path ()
  "Append a directory to the PYTHONPATH."
  (interactive
   (let ((string (read-directory-name 
          "Python package directory: " 
          nil 
          'my-history)))
     (setenv "PYTHONPATH" (concat (expand-file-name string)
                  (getenv ":PYTHONPATH"))))))

(defun pythonpath-dired-mode-hook ()
  (let ((setup_py (concat default-directory "setup.py"))
    (directory (expand-file-name default-directory)))
    ;;   (if (file-exists-p setup_py)
    (if (is-python-package-directory directory)
    (let ((pythonpath (concat (getenv "PYTHONPATH") ":" 
                  (expand-file-name directory))))
      (setenv "PYTHONPATH" pythonpath)
      (message (concat "PYTHONPATH=" (getenv "PYTHONPATH")))))))

(defun is-python-package-directory (directory)
  (let ((setup_py (concat directory "setup.py")))
    (file-exists-p setup_py)))

(add-hook 'dired-mode-hook 'pythonpath-dired-mode-hook)

Hope this helps.

不知在何时 2024-08-21 08:33:55

关键是将项目目录添加到 sys.path 中,而不考虑 env 变量。

对于使用 VSCode 的人来说,如果您的项目有基目录,这里有一个单行解决方案:

[MASTER]
init-hook='base_dir="my_spider"; import sys,os,re; _re=re.search(r".+\/" + base_dir, os.getcwd()); project_dir = _re.group() if _re else os.path.join(os.getcwd(), base_dir); sys.path.append(project_dir)'

让我解释一下:

  • 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:

[MASTER]
init-hook='base_dir="my_spider"; import sys,os,re; _re=re.search(r".+\/" + base_dir, os.getcwd()); project_dir = _re.group() if _re else os.path.join(os.getcwd(), base_dir); sys.path.append(project_dir)'

Let me explain it a little bit:

  • re.search(r".+\/" + base_dir, os.getcwd()).group(): find base directory according to the editing file

  • os.path.join(os.getcwd(), base_dir): add cwd to sys.path to meet command line environment


FYI, here's my .pylintrc:

https://gist.github.com/chuyik/f0ffc41a6948b6c87c7160151ffe8c2f

百思不得你姐 2024-08-21 08:33:55

我遇到了同样的问题,并通过在 virtualenv 中安装 pylint 来修复它,然后将 .pylintrc 文件添加到我的项目目录中,文件中包含以下内容:

[Master]
init-hook='sys.path = list(); sys.path.append("./Lib/site-packages/")'

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:

[Master]
init-hook='sys.path = list(); sys.path.append("./Lib/site-packages/")'
安穩 2024-08-21 08:33:55

安装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

生来就爱笑 2024-08-21 08:33:55

首先,转到您的 VS Code,然后按“ctrl + shift + p”

然后搜索 settings.json

然后将以下代码粘贴到 settings.jason 中。我希望问题能够得到解决。

{

"python.pythonPath": "venv/bin/python",
"python.linting.pylintPath": "venv/bin/pylint"

}

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.

{

"python.pythonPath": "venv/bin/python",
"python.linting.pylintPath": "venv/bin/pylint"

}

国际总奸 2024-08-21 08:33:55

我在尝试提交 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.

国际总奸 2024-08-21 08:33:55

这一切对我有用!

.pylintrc

[MASTER]
; init-hook='import sys; sys.path.append("./venv/lib/python3.8/site-packages")'

; or
init-hook='import sys; sys.path.append(f"./venv/lib/python{sys.version[:3]}/site-packages");'

; or
;init-hook='from distutils.sysconfig import get_python_lib; sys.path.append(get_python_lib())'

; or
; init-hook='import site; sys.path.append(site.getsitepackages()[0])'

this all work for me!

.pylintrc

[MASTER]
; init-hook='import sys; sys.path.append("./venv/lib/python3.8/site-packages")'

; or
init-hook='import sys; sys.path.append(f"./venv/lib/python{sys.version[:3]}/site-packages");'

; or
;init-hook='from distutils.sysconfig import get_python_lib; sys.path.append(get_python_lib())'

; or
; init-hook='import site; sys.path.append(site.getsitepackages()[0])'
鹿港小镇 2024-08-21 08:33:55

我刚刚发现的一种解决方法是实际上只为整个包运行 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.

不顾 2024-08-21 08:33:55

这是一个老问题,但没有公认的答案,所以我建议这样做:将two.py中的导入语句更改为:

from .. import one

在我当前的环境(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:

from .. import one

In my current environment (Python 3.6, VSCode using pylint 2.3.1) this clears the flagged statement.

淡莣 2024-08-21 08:33:55

我发现这在具有 pipelinenv 虚拟环境的本地 .pylintrc 文件中运行良好:

[MASTER]
init-hook='import site; sys.path += site.getsitepackages()'

请参阅 这篇文章了解有关站点包的信息。

I found this worked nicely in my local .pylintrc file with a pipenv virtual environment:

[MASTER]
init-hook='import site; sys.path += site.getsitepackages()'

See this post for info about site packages.

踏雪无痕 2024-08-21 08:33:55

我遇到了同样的问题并找到了不同的解决方案。这里已经提出的一些解决方案对我来说不起作用或不合需要。

  • 首先,我使用 pylint 版本 3.0.3 并且 find_pylintrc 不再存在。
  • 我不想在 init-root 中使用绝对路径。
  • 我想要独立于 IDE,因此无法调整 PyCharm 配置。
  • 我希望它被提交到存储库,因此它必须为其他人工作并且不能包含绝对路径。

我的解决方案是将以下代码段添加到 pylintrc 文件的 MAIN 部分。 (如果您使用不同版本的 pylint,您的内容可能与 MAIN 不同)。

[MAIN]
source-roots=repository/apps,src/apps

源根目录列表可以是绝对的,也可以是相对于调用 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.

  • First, I am using pylint version 3.0.3 and find_pylintrc is not present anymore.
  • I didn't want to use absolute paths in init-root.
  • I wanted to be IDE independent, so tweaking PyCharm configuration is not possible.
  • I wanted it to be committed to repository so it had to work for others and can't contain absolute paths.

The solution in my case was to add the following snippet to the MAIN section of the pylintrc file. (You may have something different than MAIN if you are using a different version of pylint).

[MAIN]
source-roots=repository/apps,src/apps

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 case pylint is called from within the IDE.

很糊涂小朋友 2024-08-21 08:33:55

如果有人正在寻找一种方法来将 pylint 作为 PyCharm 中的外部工具运行并使其与他们的虚拟环境一起工作(为什么我提出这个问题),以下是我解决它的方法:

  1. 在 PyCharm > 中首选项>工具>外部工具,添加或编辑 pylint 的项目。
  2. 在“编辑工具”对话框的“工具设置”中,将程序设置为使用 python 解释器目录中的 pylint: $PyInterpreterDirectory$/pylint
  3. 在“参数”字段中设置其他参数,例如:--rcfile =$ProjectFileDir$/pylintrc -rn $FileDir$
  4. 将工作目录设置为 $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:

  1. In PyCharm > Preferences > Tools > External Tools, Add or Edit an item for pylint.
  2. In the Tool Settings of the Edit Tool dialog, set Program to use pylint from the python interpreter directory: $PyInterpreterDirectory$/pylint
  3. Set your other parameters in the Parameters field, like: --rcfile=$ProjectFileDir$/pylintrc -r n $FileDir$
  4. Set your working directory to $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).

蓝礼 2024-08-21 08:33:55

Mac 用户:如果您使用 Anaconda 3 和 vsCode 并拥有多个环境,则通过 settings.json 指向 vsCode 的以下路径也可以:

{
  "python.pythonPath": "/Users/username/opt/anaconda3/bin/python",
  "python.linting.pylintPath": "/Users/username/opt/anaconda3/bin/python"
}

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:

{
  "python.pythonPath": "/Users/username/opt/anaconda3/bin/python",
  "python.linting.pylintPath": "/Users/username/opt/anaconda3/bin/python"
}
娇女薄笑 2024-08-21 08:33:55

如果您使用的是 Windows:

  • 获取刚刚创建的虚拟环境中 python.exe 的路径,
  • 它应该像这样 Z:\YourProjectFolder\Scripts\python.exe
  • 然后转到您的 vscode 并编辑用户 settings.json
  • 添加此行: "python.pythonPath": "Z:\\YourProjectFolder\\Scripts\\python.exe"
  • 保存它并且应该可以修复问题
  • 注意:将双反斜杠而不是单反斜杠放入 json 文件时
{
    "python.pythonPath": "Z:\\YourProjectFolder\\Scripts\\python.exe"
}

If you are using Windows:

  • get the path of the python.exe inside the virtual environment you just created
  • it should be like this Z:\YourProjectFolder\Scripts\python.exe
  • then go to your vscode and edit the user settings.json
  • add this line: "python.pythonPath": "Z:\\YourProjectFolder\\Scripts\\python.exe"
  • save it and that should fix the issue
  • NOTE: the double backslash instead of single when putting it to the json file
{
    "python.pythonPath": "Z:\\YourProjectFolder\\Scripts\\python.exe"
}
剩余の解释 2024-08-21 08:33:55

您好,我能够从不同的目录导入包。我刚刚做了以下事情:
注意:我正在使用 VScode

创建 Python 包的步骤
使用 Python 包非常简单。您需要做的就是:

  • 创建一个目录并为其指定包的名称。
  • 把你的课程放进去。
  • 在目录中创建一个 __init__.py 文件

例如:您有一个名为 Framework 的文件夹,其中保存所有自定义类,您的工作就是在名为的文件夹中创建一个 __init__.py 文件框架。

在导入时,您需要以这种方式导入--->

from Framework import base

这样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:

  • Create a directory and give it your package's name.
  • Put your classes in it.
  • Create a __init__.py file in the directory

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--->

from Framework import base

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!!!!

恋竹姑娘 2024-08-21 08:33:55

对于 pylint >=3.0,find_pylintrc 已被删除,您需要执行以下操作:

[MASTER]
init-hook="from pylint.config import find_default_config_files; import sys; sys.path.append(next(find_default_config_files()).parent.as_posix())"

这使用新的 find_default_config_files 函数。

For pylint >=3.0 the find_pylintrc has been removed, instead you'll need to do something like:

[MASTER]
init-hook="from pylint.config import find_default_config_files; import sys; sys.path.append(next(find_default_config_files()).parent.as_posix())"

This uses the new find_default_config_files function.

仙气飘飘 2024-08-21 08:33:55

也许通过手动将目录附加到 PYTHONPATH 中?

sys.path.append(dirname)

Maybe by manually appending the dir inside the PYTHONPATH?

sys.path.append(dirname)
我恋#小黄人 2024-08-21 08:33:55

如果您使用 vscode,请确保您的包目录位于 _pychache__ 目录之外。

if you using vscode,make sure your package directory is out of the _pychache__ directory.

皇甫轩 2024-08-21 08:33:54

我知道有两种选择。

一,更改 PYTHONPATH 环境变量以包含模块上方的目录。

,编辑 ~/.pylintrc 以包含模块上方的目录,如下所示:(

[MASTER]
init-hook='import sys; sys.path.append("/path/to/root")'

或者在 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:

[MASTER]
init-hook='import sys; sys.path.append("/path/to/root")'

(Or in other version of pylint, the init-hook requires you to change [General] to [MASTER])

Both of these options ought to work.

[旋木] 2024-08-21 08:33:54

init-hook 中更改路径的解决方案很好,但我不喜欢必须在其中添加绝对路径,因此我无法在项目的开发人员之间共享此 pylintrc 文件。使用 pylintrc 文件相对路径的解决方案对我来说效果更好:

[MASTER]
init-hook="from pylint.config import find_pylintrc; import os, sys; sys.path.append(os.path.dirname(find_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:

[MASTER]
init-hook="from pylint.config import find_pylintrc; import os, sys; sys.path.append(os.path.dirname(find_pylintrc()))"

Note that pylint.config.PYLINTRC also exists and has the same value as find_pylintrc().

烟雨凡馨 2024-08-21 08:33:54

通过在venv下配置pylint路径可以解决该问题:
$ cat .vscode/settings.json

{
    "python.pythonPath": "venv/bin/python",
    "python.linting.pylintPath": "venv/bin/pylint"
}

The problem can be solved by configuring pylint path under venv:
$ cat .vscode/settings.json

{
    "python.pythonPath": "venv/bin/python",
    "python.linting.pylintPath": "venv/bin/pylint"
}
情深缘浅 2024-08-21 08:33:54

我在项目的根目录中添加了一个新文件 pylintrc

[MASTER]
init-hook='import sys; sys.path.append(".")'

它在 PyCharm IDE 中适用于我

I've added a new file pylintrc in the project's root directory with

[MASTER]
init-hook='import sys; sys.path.append(".")'

and it works for me in PyCharm IDE

蓬勃野心 2024-08-21 08:33:54

创建 .pylintrc 并添加

[MASTER]
init-hook="from pylint.config import find_pylintrc;
import os, sys; sys.path.append(os.path.dirname(find_pylintrc()))"

Create .pylintrc and add

[MASTER]
init-hook="from pylint.config import find_pylintrc;
import os, sys; sys.path.append(os.path.dirname(find_pylintrc()))"
把回忆走一遍 2024-08-21 08:33:54

两个目录中是否都有一个空的 __init__.py 文件来让 python 知道这些目录是模块?

当您不从文件夹内运行时(即可能从 pylint 的文件夹中运行,尽管我没有使用过),基本轮廓是:

topdir\
  __init__.py
  functions_etc.py
  subdir\
    __init__.py
    other_functions.py

这就是 python 解释器在不引用当前目录的情况下识别模块的方式,因此如果 pylint 是从自己的绝对路径运行,它将能够以 topdir.functions_etctopdir.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:

topdir\
  __init__.py
  functions_etc.py
  subdir\
    __init__.py
    other_functions.py

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 as topdir.functions_etc or topdir.subdir.other_functions, provided topdir is on the PYTHONPATH.

UPDATE: If the problem is not the __init__.py file, maybe just try copying or moving your module to c:\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.

故事↓在人 2024-08-21 08:33:54

1) sys.path 是一个列表。

2) 问题有时是 sys.path 不是你的 virtualenv.path 并且你想在你的 virtualenv 中使用 pylint

3) 所以就像说的,使用 init-hook (注意 ' 和 " 中的pylint 的解析是严格的)

[Master]
init-hook='sys.path = ["/path/myapps/bin/", "/path/to/myapps/lib/python3.3/site-packages/", ... many paths here])'

[Master]
init-hook='sys.path = list(); sys.path.append("/path/to/foo")'

.. 和

pylint --rcfile /path/to/pylintrc /path/to/module.py

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)

[Master]
init-hook='sys.path = ["/path/myapps/bin/", "/path/to/myapps/lib/python3.3/site-packages/", ... many paths here])'

or

[Master]
init-hook='sys.path = list(); sys.path.append("/path/to/foo")'

.. and

pylint --rcfile /path/to/pylintrc /path/to/module.py
轮廓§ 2024-08-21 08:33:54

我不知道它如何与 WingIDE 一起使用,但是为了将 PyLint 与 Geany 一起使用,我将外部命令设置为:

PYTHONPATH=${PYTHONPATH}:$(dirname %d) pylint --output-format=parseable --reports=n "%f"

其中 %f 是文件名,%d 是路径。可能对某人有用:)

I don't know how it works with WingIDE, but for using PyLint with Geany, I set my external command to:

PYTHONPATH=${PYTHONPATH}:$(dirname %d) pylint --output-format=parseable --reports=n "%f"

where %f is the filename, and %d is the path. Might be useful for someone :)

一花一树开 2024-08-21 08:33:54

我必须更新系统 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.

木有鱼丸 2024-08-21 08:33:54

Try

if __name__ == '__main__':
    from [whatever the name of your package is] import one
else:
    import one

请注意,在 Python 3 中,else 子句中部分的语法将是

from .. import one

再想一想,这可能无法修复您的问题具体问题。我误解了这个问题,认为two.py 正在作为主模块运行,但事实并非如此。考虑到 Python 2.6(不从 __future__ 导入 absolute_import)和 Python 3.x 处理导入方式的差异,无论如何,您都不需要为 Python 2.6 执行此操作,我不认为。

尽管如此,如果您最终切换到 Python 3 并计划使用模块作为包模块和包内的独立脚本,那么保留它可能是一个好主意
东西

if __name__ == '__main__':
    from [whatever the name of your package is] import one   # assuming the package is in the current working directory or a subdirectory of PYTHONPATH
else:
    from .. import one

类似心里的

。编辑:现在为您的实际问题提供可能的解决方案。要么从包含 one 模块的目录运行 PyLint(也许通过命令行),或者在运行 PyLint 时将以下代码放在某处:

import os

olddir = os.getcwd()
os.chdir([path_of_directory_containing_module_one])
import one
os.chdir(olddir)

基本上,作为摆弄 PYTHONPATH 的替代方法,只需确保当前工作目录是执行导入时包含 one.py 的目录。

(看看 Brian 的答案,您可能可以将前面的代码分配给 init_hook,但如果您打算这样做,那么您可以简单地附加到 sys.path他确实这么做了,这比我的解决方案稍微优雅一些​​。)

Try

if __name__ == '__main__':
    from [whatever the name of your package is] import one
else:
    import one

Note that in Python 3, the syntax for the part in the else clause would be

from .. import one

On 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

if __name__ == '__main__':
    from [whatever the name of your package is] import one   # assuming the package is in the current working directory or a subdirectory of PYTHONPATH
else:
    from .. import one

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:

import os

olddir = os.getcwd()
os.chdir([path_of_directory_containing_module_one])
import one
os.chdir(olddir)

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 to sys.path that he does, which is slightly more elegant than my solution.)

千寻… 2024-08-21 08:33:54

我找到了一个很好的答案。编辑你的 pylintrc 并在 master 中添加以下内容

init-hook="import imp, os; from pylint.config import find_pylintrc; imp.load_source('import_hook', os.path.join(os.path.dirname(find_pylintrc()), 'import_hook.py'))"

I found a nice answer. Edit your pylintrc and add the following in master

init-hook="import imp, os; from pylint.config import find_pylintrc; imp.load_source('import_hook', os.path.join(os.path.dirname(find_pylintrc()), 'import_hook.py'))"
祁梦 2024-08-21 08:33:54

如果您想从传递给 pylint 的当前模块/文件开始查找模块的根,这将做到这一点。

[MASTER]
init-hook=sys.path += [os.path.abspath(os.path.join(os.path.sep, *sys.argv[-1].split(os.sep)[:i])) for i, _ in enumerate(sys.argv[-1].split(os.sep)) if os.path.isdir(os.path.abspath(os.path.join(os.path.sep, *sys.argv[-1].split(os.sep)[:i], '.git')))][::-1]

如果你有一个 python 模块 ~/code/mymodule/ ,具有这样的顶级目录布局

~/code/mymodule/
├── .pylintrc
├── mymodule/
│   └── src.py
└── tests/
    └── test_src.py

那么这会将 ~/code/mymodule/ 添加到你的 python 路径并允许 pylint 在您的 IDE 中运行,即使您在 tests/test_src.py 中导入 mymodule.src 也是如此。

您可以将检查替换为 .pylintrc,但是当涉及到 Python 模块的根目录时,git 目录通常是您想要的。

在你提问之前

答案使用import sys, os; sys.path.append(...) 缺少一些可以证明我的答案格式合理的东西。我通常不会以这种方式编写代码,但在这种情况下,您将不得不处理 pylintrc 配置解析器和评估器的限制。它实际上在 init_hook 回调的上下文,因此任何导入 pathlib、使用多行语句、将某些内容存储到变量等的尝试都将不起作用。

我的代码的一种不太令人厌恶的形式可能如下所示:

import os
import sys

def look_for_git_dirs(filename):
    has_git_dir = []
    filename_parts = filename.split(os.sep)
    for i, _ in enumerate(filename_parts):
        filename_part = os.path.abspath(os.path.join(os.path.sep, *filename_parts[:i]))
        if os.path.isdir(os.path.join(filename_part, '.git')):
            has_git_dir.append(filename_part)
    return has_git_dir[::-1]

# don't use .append() in case there's < 1 or > 1 matches found
sys.path += look_for_git_dirs(sys.argv[-1])

我希望我可以使用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.

[MASTER]
init-hook=sys.path += [os.path.abspath(os.path.join(os.path.sep, *sys.argv[-1].split(os.sep)[:i])) for i, _ in enumerate(sys.argv[-1].split(os.sep)) if os.path.isdir(os.path.abspath(os.path.join(os.path.sep, *sys.argv[-1].split(os.sep)[:i], '.git')))][::-1]

If you have a python module ~/code/mymodule/, with a top-level directory layout like this

~/code/mymodule/
├── .pylintrc
├── mymodule/
│   └── src.py
└── tests/
    └── test_src.py

Then this will add ~/code/mymodule/ to your python path and allow for pylint to run in your IDE, even if you're importing mymodule.src in tests/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 runs exec in the context of the init_hook callback so any attempt to import pathlib, use multi-line statements, store something into variables, etc., won't work.

A less disgusting form of my code might look like this:

import os
import sys

def look_for_git_dirs(filename):
    has_git_dir = []
    filename_parts = filename.split(os.sep)
    for i, _ in enumerate(filename_parts):
        filename_part = os.path.abspath(os.path.join(os.path.sep, *filename_parts[:i]))
        if os.path.isdir(os.path.join(filename_part, '.git')):
            has_git_dir.append(filename_part)
    return has_git_dir[::-1]

# don't use .append() in case there's < 1 or > 1 matches found
sys.path += look_for_git_dirs(sys.argv[-1])

I wish I could have used pathlib.Path(filename).parents it would have made things much easier.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文