我在Windows上的Wing IDE内部运行PyLint。我有一个子目录(包)在我的项目和包内,我从顶层导入一个模块,即。

__init__.py
myapp.py
one.py
subdir\
    __init__.py
    two.py

在two.py中,我导入了一个,这在运行时工作得很好,因为顶层目录(myapp.py从其中运行)在Python路径中。然而,当我在two.py上运行PyLint时,它会给我一个错误:

F0401: Unable to import 'one'

我怎么解决这个问题?


当前回答

我也有同样的问题,因为我找不到答案,我希望这可以帮助任何有类似问题的人。

我使用flymake与附页。基本上,我所做的就是添加一个dired-mode-hook来检查dired目录是否是python包目录。如果是,我将它添加到PYTHONPATH。在我的例子中,我认为一个目录是一个python包,如果它包含一个名为“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)

希望这能有所帮助。

其他回答

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

当你不是从文件夹中运行时(比如可能从pylint的文件夹中运行,尽管我没有用过),基本的大纲是:

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

这就是python解释器如何在不引用当前目录的情况下感知模块,因此如果pylint从它自己的绝对路径运行,它将能够以topdir的身份访问functions_etc.py。Functions_etc或topdir.subdir。other_functions,前提是topdir在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.

也许通过手动在PYTHONPATH中追加目录?

sys.path.append(dirname)

首先,进入VS Code,然后按“ctrl + shift + p”

然后搜索settings.json

然后将下面的代码粘贴到设置中。我希望问题能够解决。

{

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

}

在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()相同的值。

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

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

创建一个目录,并给它您的包的名称。 把你的课程放在里面。 在该目录中创建__init__.py文件

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

在导入时,你需要以这种方式导入——>

from Framework import base

因此E0401错误消失 Framework是你刚刚创建__init__.py和 Base是您需要导入并处理的自定义模块 希望能有所帮助!!!!