user1394
user1394

Reputation: 588

Adding source root to Python project structure VS Code

It seems that this issue has been a common one for quite some time, but after scouring stackoverflow, reddit, and vscode docs, I am desperate to find an answer. I thought I'd give it another shot here. In PyCharm, you can go to 'Project Structure' and add source root folders to resolve relative import and pylint issues, as described here: https://www.jetbrains.com/help/pycharm/configuring-project-structure.html.

Is there a way to implement this feature in VSCode?

Upvotes: 7

Views: 6323

Answers (3)

Ling
Ling

Reputation: 555

Here's what I found after quite some time searching on this issue.

  • For simple workspace, one can search extraPath in settings, and add the relative path to "Python > Analysis: Extra Paths". Assuming the latest version of vscode and pylance.
  • For multi-root workspace, it turns out Microsoft is still improving or even debating on how PYTHONPATH should be obtained (see https://github.com/microsoft/pylance-release/issues/159). If you want to reference in one folder modules from other folders, I personally still prefer the aforementioned extraPath --- just this time you'll put in relative path (on disk) to your current folder, something like ../<your another folder>/src/python. And the settings is in the "Folder Settings" instead of "User" or "Workspace" Settings. This also has to be done for each and every root folder.

Hope the above helps.

Upvotes: 0

andyhasit
andyhasit

Reputation: 15319

I just wasted 2 hours trying to get pylint to recognise modules imported from local paths, but finally got it working.

I added the root to "python.autoComplete.extraPaths" but that only affected autocompletion, not pylint!

For that I had to install the project in the virtualenv.

First things first, make sure you are pointing to the interpreter in your virtualenv. Here is what my VSCODE workspace settings look like:

{
  "python.linting.pylintEnabled": true,
  "python.pythonPath": "/home/andrew/virtualenvs/eks/bin/python",
  "python.linting.pylintPath": "/home/andrew/virtualenvs/eks/bin/pylint",
  "python.autoComplete.extraPaths": ["/other/repos/eks"],
  "python.linting.pylintArgs": [
    "--disable=C0111", // missing docstring
    "--load-plugins=pylint_django"
  ],
}

Second, with the virtualenv active, install the current directory as symlink like so:

pip install -e .

For the project to be installable, you need a setup.py file, which need only look like this:

from setuptools import setup

setup()

Of course you can use the setup mechanism with a setup.cfg file to control dependencies, but if you're having this problem then you are most likely not doing that in your project, so the above is the minimum you need to get it to work.

And of course, it all makes sense. How was pylint pointed to the python interpreter in your virtualenv supposed to know about a rot directory elsewhere?

Upvotes: 3

Brett Cannon
Brett Cannon

Reputation: 16070

It sounds like you're after the "python.autoComplete.extraPaths" setting which lets you list an array of paths to look for packages in for auto-complete. As for Pylint, setting up your PYTHONPATH via a .env file is probably what you're after.

Upvotes: 1

Related Questions