6

I'm trying to add the src folder to my PYTHONPATH i.E. do the same thing as Mark directory as sources root in PyCharm.

I've looked at the solutions to Visual Studio Code - How to add multiple paths to python path? but the solutions shown there do not work for me.

I've set up my minimal example as follows shown in the screenshot below:

enter image description here

My understanding would be that having a .env file with PYTHONPATH="./src;${PYTHONPATH}" would add the src file to the path.

Nevertheless, when running the code runner or when running python change_pyhton_path.py src is not part of the PYTHONPATH and the direct import from src fails.

I do have setting "python.envFile": "${workspaceFolder}/.env".

In pyCharm on the other hand everything works as it should after hitting Mark directory as source on src. enter image description here

Thanks for helping out!

Hellvetia
  • 335
  • 3
  • 11

3 Answers3

3

in your settings.json add:

"terminal.integrated.env.windows": {
    "PYTHONPATH": "full python path here"
}
Szabolcs Dombi
  • 5,493
  • 3
  • 39
  • 71
0

if you have problems with importing modules and you are using code-runner, try to add

"code-runner.fileDirectoryAsCwd": true

to your settings.json file

Casey Fu
  • 11
  • 1
0

If you are trying to get auto complete working from your source directory, you could add to the PYTHONPATH environment variable as you are doing. You can also go the "vscode native" route, as there is a configuration. Open your workspace settings and add the following line:

"python.autoComplete.extraPaths": ["./src"]

NOTE: Avoid setting this at the user level as each project differs in where the source code lives

lucasnad27
  • 11
  • 2