PyLint "Unable to import" error - how to set PYTHONPATH?

ghz 3months ago ⋅ 114 views

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?

Answers

This issue occurs because PyLint, by default, might not be aware of the correct Python path when running from subdirectories, particularly in IDEs like Wing IDE. While the imports work during runtime, PyLint needs explicit guidance about the top-level directory being part of the Python path.

Solution: Update the Python Path for PyLint

You can fix this issue by either setting the PYTHONPATH environment variable or by configuring PyLint directly.

Method 1: Set PYTHONPATH Environment Variable

You can add the top-level directory of your project to PYTHONPATH so that PyLint can locate it properly.

  1. For Windows (temporary solution for current session): In the command line or in the Wing IDE's environment configuration (if available), set PYTHONPATH:

    set PYTHONPATH=<path_to_top_level_directory>
    

    For example, if your project is located at C:\path\to\project, you would run:

    set PYTHONPATH=C:\path\to\project
    

    After setting this, run PyLint again and it should find one.py without errors.

  2. For Windows (permanent solution): You can also set the PYTHONPATH environment variable globally on your system:

    • Right-click My Computer or This PC, and choose Properties.
    • Select Advanced system settings.
    • Click Environment Variables.
    • Under System Variables, click New to add a new variable.
    • Set the variable name as PYTHONPATH and the value as the path to your project's root directory (e.g., C:\path\to\project).
    • Restart Wing IDE after this to ensure the changes take effect.

Method 2: Create a .pylintrc Configuration File

You can configure PyLint directly by specifying the Python path in a configuration file.

  1. Generate a .pylintrc file in your project root if you don't already have one:

    pylint --generate-rcfile > .pylintrc
    
  2. Open the .pylintrc file and find the init-hook option under the [MASTER] section. Set it to add the top-level directory to sys.path:

    [MASTER]
    init-hook='import sys; sys.path.append(r"C:\path\to\project")'
    

    Replace C:\path\to\project with the path to the root of your project.

  3. Save the file, and PyLint should now properly resolve imports from the top-level directory.

Method 3: Use --init-hook Flag in PyLint Command

If you are running PyLint manually, you can also use the --init-hook flag:

pylint --init-hook="import sys; sys.path.append(r'C:\path\to\project')" subdir/two.py

This will add the top-level project directory to the sys.path at runtime when PyLint runs.

Summary

The issue is caused by PyLint not automatically recognizing the top-level directory in your Python path. By explicitly adding the top-level directory to PYTHONPATH, either through the environment variable or configuration, PyLint will correctly locate the modules, and the import errors should disappear.