7

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!

0

4 Answers 4

3

in your settings.json add:

"terminal.integrated.env.windows": {
    "PYTHONPATH": "full python path here"
}
Sign up to request clarification or add additional context in comments.

1 Comment

This did not solve my issue. The behavior remained unchanged.
1

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

Comments

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

1 Comment

Can you include inline code span so the code is readable & verifiable.
0

Adding

"env": { "PYTHONPATH": "${workspaceFolder}" },

to your launch.json could solve the problem

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.