PyLint Error “Failed to import” - how to install PYTHONPATH?

I am running PyLint from the internal Wing IDE on Windows. I have a subdirectory (package) in my project, and inside the package I import the module from the top level, i.e.

__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 myapp.py ) is in the Python path. However, when I run PyLint on two.py, it gives me an error:

 F0401: Unable to import 'one' 

How to fix it?

+124
python virtualenv pythonpath pylint
Dec 14 '09 at 7:22
source share
19 answers

There are two options that I know of.

First, change the PYTHONPATH environment variable to include the directory above your module.

Or edit ~/.pylintrc to include the directory above your module, for example:

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

(or in another version of pylint, the initialization trap requires you to change [General] to [MASTER])

Both of these options should work.

Hope this helps.

+118
Jun 17 '10 at 19:46
source share

1) sys.path is a list.

2) The problem is that sys.path is not your virtualenv.path and you want to use pylint in your virtualenv

3) So, as said, use init-hook (note the 'and "pylint parsing strictly)

 [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 
+24
Apr 29 '14 at 21:06
source share

The decision to change the path in the init-hook is a good one, but I don’t like the fact that I had to add an absolute path there, as a result I can not share this pylintrc file among the project developers. This solution, using the relative path to the 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 meaning as find_pylintrc() .

+23
Aug 29 '16 at 13:15
source share

Do you have an empty __init__.py file in both directories to let python know that dirs are modules?

The main circuit when you are not working from a folder (i.e., maybe from pylint, although I did not use it):

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

So the python interpreter knows the module without reference to the current directory, so if pylint is working from its absolute path, it will be able to access functions_etc.py like topdir.functions_etc or topdir.subdir.other_functions , provided that topdir is on PYTHONPATH .

UPDATE: if the problem is not in the __init__.py file, maybe just try copying or moving your module to c:\Python26\Lib\site-packages - this is the usual place to place additional packages and will certainly be in your python path. If you know how to make Windows symbolic links or equivalent (I do not!), You can do it instead. There are many more options here: http://docs.python.org/install/index.html , including the ability to add sys.path with the level directory of your development code, but in practice I usually just symbolically link my local development directory to sites- in batches - copying it has the same effect.

+17
Dec 14 '09 at 7:45
source share

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

 { "python.pythonPath": "venv/bin/python", "python.linting.pylintPath": "venv/bin/pylint" } 
+11
Dec 24 '18 at 2:22
source share

I do not know how this works with WingIDE, but to use PyLint with Geany I installed an external command:

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

where% f is the file name and% d is the path. May be useful to someone :)

+5
Nov 12 '10 at 8:12
source share

I had to update the system variable PYTHONPATH to add my path to the App Engine. In my case, I just had to edit the ~/.bashrc and add the following line:

export PYTHONPATH=$PYTHONPATH:/path/to/google_appengine_folder

Actually, I first tried to install init-hook , but this did not help to solve the problem sequentially in my code base (I don’t know why). As soon as I added it to the system path (probably a good idea in general), my problems were gone.

+4
Sep 11 '16 at 15:30
source share

One workaround I just opened up is to simply run PyLint for the whole package, not just a single file. One way or another, he will be able to find the imported module.

+2
Dec 16 '09 at 6:15
source share

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

 from .. import one 

Strike>

On the other hand, this probably will not solve your specific problem. I did not understand this question and thought that two.py starts as the main module, but it is not. And given the differences in the way Python 2.6 (without importing absolute_import from __future__ ) and importing Python 3.x pens, you won’t need to do this for Python 2.6 anyway, I don’t think so.

However, if you end up switching to Python 3 and plan to use the module as a package module and as a stand-alone script inside a package, it might 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 real problem. Either start PyLint from the directory containing your one module (possibly using the command line), or put the following code somewhere when starting PyLint:

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

Basically, as an alternative to messing around with PYTHONPATH, just make sure the current working directory is the directory containing one.py when you import.

(Looking at Brian’s answer, you could probably assign the previous init_hook code, but if you do, you can just make an addition to sys.path , which it does, which is a bit more elegant than my solution.)

+2
Jun 17 '10 at 20:03
source share

The key is to add the project directory to sys.path without regard to the env variable.

For those using VSCode, here is a one-line solution for you if there is a base directory for 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 this a bit:

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

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




FYI, here is my .pylintrc:

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

+2
May 15 '16 at
source share

I had the same problem and fixed it by installing pylint in my virtualenv and then adding the .pylintrc file to the project directory with the following in the file:

 [Master] init-hook='sys.path = list(); sys.path.append("./Lib/site-packages/")' 
+2
Jul 26 '16 at 17:17
source share

Maybe manually adding a directory to PYTHONPATH?

 sys.path.append(dirname) 
+1
Dec 15 '09 at 12:08
source share

I had the same problem, and since I could not find the answer, I hope this can help anyone who has a similar problem.

I use flymake with epilin. Basically what I did was add a standby binding that checks if the legacy directory is a python package directory. If I add it to PYTHONPATH. In my case, I believe the directory is a python package if it contains a file called "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.

+1
Nov 09 '13 at 23:11
source share

If someone is looking for a way to run pylint as an external tool in PyCharm and work with virtual environments (why I came to this question), here is how I solved it:

  • In PyCharm> Preferences> Tools> External Tools, add or edit an item for pylint.
  • In the Tools dialog box of the Edit Tool dialog box, set the Program to use pylint from the python interpreter directory: $PyInterpreterDirectory$/pylint
  • Set other parameters in the Parameters field, for example: --rcfile=$ProjectFileDir$/pylintrc -rn $FileDir$
  • Set the working directory to $FileDir$

Now, using pylint as an external tool, run pylint in any directory that you have selected, using a common configuration file and using any interpreter configured for your project (which is supposedly your virtualenv interpreter).

+1
May 2 '16 at 16:15
source share

I found a good answer from https://sam.hooke.me/note/2019/01/call-python-script-from-pylint-init-hook/ edit your pylintrc and add the following to 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'))"

+1
Sep 17 '19 at 17:51
source share

if you are using vscode, make sure your package directory is outside the _pychache__ directory.

0
Nov 21 '18 at 8:15
source share

This is an old question, but there is no accepted answer, so I suggest: change the import statement in two.py so that it reads like this:

 from .. import one 

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

0
Aug 23 '19 at 18:20
source share

If you are using Cython on Linux, I decided to remove the module.cpython-XXm-X-linux-gnu.so files from the target directory of my project.

0
Sep 11 '19 at 12:57
source share

Hello, I was able to import packages from another directory. I just did the following: Note. I am using VScode

Steps to Create a Python Package Working with Python packages is really easy. All you have to do is:

Create a directory and give it the name of your package. Put your classes on it. Create an init .py file in the directory

.For example: you have a folder named Framework in which you store all custom classes, and your task is to simply create an init .py file inside a folder named Framework.

And when importing, you need to import into this fashion --->

from the framework import database

so that error E0401 disappears Framework - this is the folder in which you just created init .py and base - your custom module into which you should import and work on. I hope this helps !!!!

-one
Jul 09 '18 at 7:01
source share



All Articles