Python PDM + pre-commit using pylint: imports cannot be found

1.4k views Asked by At

Background

I am wrangling some legacy code into shape.

I use PDM to manage dependencies, which places all dependent packages in a __pypackages__ folder directly under the repo root level. PDM also uses the relatively new pyproject.toml package config file.

I am trying to adopt pre-commit Git hooks so that I can have automated checks for formatting and style before trying to commit, merge, and/or create PRs.

I am asking pre-commit to use only a few Python tools for now: pylint and black.

Issue

Most of that toolset works great together. However, pylint cannot find any of the modules that are stored in the __pypackages__ folder. Most of what I have read suggests that I alter my $PYTHONPATH to find the modules.

This solution seems very outdated. But also, I am not sure how I can do this in a robust way across the team. I can alter the Git hooks, but the $PYTHONPATH may be different for each engineer, so this will only work for my machine.

I would like to be able to add something in the pyproject.toml file to have pylint find it. I am not sure what to write, though, so that it generically works across the whole team. Something like

[tools.pylint]
pypackages = "./__pypackages__"

Any ideas how I can do this?

Details

I am not sure more details are needed, but here it is:

My actions:

> pre-commit run --all-files # The --all-files flag is just to allow me to test without a commit
Trim Trailing Whitespace.................................................Passed
Fix End of Files.........................................................Passed
Check Yaml...........................................(no files to check)Skipped
Check for added large files..............................................Passed
black....................................................................Passed
pylint...................................................................Failed
- hook id: pylint
- exit code: 30

************* Module testfile
testfile.py:18:0: E0401: Unable to import 'boto3' (import-error)

boto3 is in the __pypackages__ mentioned above. None of the modules can be imported, but I limited the output for clarity.

I can pdm run ... everything correctly and VS Code sees the modules fine. But pylint is not finding it because it cannot find this __pypackages__ folder.

1

There are 1 answers

1
Frost Ming On BEST ANSWER

You can get around this by updating the PYTHONPATH environment variable used by the extension, by creating a file named .env in your workspace (project folder) and adding the following entry:

PYTHONPATH=D:/commonScripts

Note: Relative paths are also supported. Further info on .env files can be found here https://code.visualstudio.com/docs/python/environments#_environment-variable-definitions-file