skip to Main Content

I recently started using Pycharm, an IDE which I have not used before.

I discovered that Pycharm has the ability to add paths to the PYTHONPATH environment variable, and that these variables will be set, by Pycharm, before running any pytests or Python scripts.

The way to do this is in the Settings:

  • Settings > Project > Project Structure
  • From here, add a subdirectory of the project as a Source Folder.
  • This can also be done from the Project filesystem tree on the left hand side of the main window. Right click a directory and go to Mark Directory as > Sources Root

Back to VS Code…

Normally when working with Python within VS Code I would follow a process very similar to the process one might use when working entirely via the command line (using something like Vim or nano as the editor):

  • Create a virtual environment using python -m venv
  • Install local source directories with pip in editable mode (requires pyproject.toml)
  • Any dependent directories become "visible" to Python scripts via the mechanism provided by pip and venv (I don’t know the full details of exactly how this works)

Alternatively…

Alternatively a more limited approach can be taken: Restrict the project structure such that all packages are contained within the same directory as the Python scripts to be run.

When a script is run, the current working directory from which it is run will be added to the PYTHONPATH by default. This means that if all dependencies are contained within the same directory, everything will just work as expected.

  • This works because no additional paths are required to be added to PYTHONPATH

Is there a way to add to PYTHONPATH in a similar manner as Pycharm does, when working with VS Code?

I suppose a possible approach would be to create a script (bash or Windows equivalent) which exports PYTHONPATH before running a Python script. I guess Pycharm must be doing something equivalent to this?

Further thoughts

One disadvantage of relying on the Pycharm method relates to deployment. If you intend to deploy your Python code to a production server at some point, then questions arise about how you would run it.

Presumably the production server will not have Pycharm installed, and certainly it will not be using Pycharm to execute a Python code.

So perhaps becoming reliant on the Pycharm behavior of marking directories as sources, causing them to be automatically added to the PYTHONPATH is not the best idea, because then a different method is required when deploying an application.

2

Answers


  1. If you are talking about the Python path then you can change the path while installing the VS Code manually instead of installing it as default. If you are talking about creating a virtual environment.
    There are many ways –

    1. cmd
    2. Python -m venv (name) –> activate
    3. ctrl+ shift + p
    Login or Signup to reply.
  2. if you want to add PYTHONPATH to your project.In vscode, you can set the configuration settings.json file. Open the .vscode/settings.json file and add these settings "python.pythonPath":"","python.envFile":"${workspaceFolder}/.env". Create a .env file in the root directory of the project and add the required PYTHONPATH to it. Or by setting it in a task.json file.Enter Tasks:Configure Task in the command panel, select Create tasks.json file from template, and select Others. In the generated tasks.json file, add the PYTHONPATH to the env object.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search