How to configure custom PYTHONPATH with VM and PyCharm?

Intellij IdeaPycharmPythonpath

Intellij Idea Problem Overview


I am using IntelliJ with the Python plugin and the Remote Interpreter feature to communicate with my Vagrant VM. It sets up the remote interpreter correctly to use my VM's interpreter. But, I use a custom PYTHONPATH in my VM, and I would like IntelliJ to recognize that path and include the modules in that path when developing.

How do I configure IntelliJ/PyCharm's remote interpreter to use a custom PYTHONPATH on the VM?

Intellij Idea Solutions


Solution 1 - Intellij Idea

For PyCharm 5 (or 2016.1), you can:

  1. select Preferences > Project Interpreter
  2. to the right of interpreter selector there is a "..." button, click it
  3. select "more..."
  4. pop up a new "Project Interpreters" window
  5. select the rightest button (named "show paths for the selected interpreter")
  6. pop up a "Interpreter Paths" window
  7. click the "+" buttom > select your desired PYTHONPATH directory (the folder which contains python modules) and click OK
  8. Done! Enjoy it!

enter image description here

enter image description here

enter image description here enter image description here

Solution 2 - Intellij Idea

Instructions for editing your PYTHONPATH or fixing import resolution problems for code inspection are as follows:

  1. Open Preferences (On a Mac the keyboard short cut is āŒ˜,).

How to get to Settings from Menu

  1. Look for Project Structure in the sidebar on the left under Project: Your Project Name

  2. Add or remove modules on the right sidebar

Project Structure Settings in Pycharm 4.5

EDIT: I have updated this screen shot for PyCharm 4.5

Solution 3 - Intellij Idea

To me the solution was to go to

Run > Edit Configuration > Defaults > Python

then manage the

  • "Add content roots to PYTHONPATH" and
  • "Add source root to PYTHONPATH"

checkboxes, as well as setting the "Working directory" field.

If you have set up your own Run/Debug Configurations then you might want to go to

Run > Edit Configuration > Python > [Whatever you called your config]

and edit it there.

My problem was that I wanted to have my whole repository included in my PyCharm 2016.2 project, but only a subfolder was the actual python source code root. I added it as "Source Root" by right clicking the folder then

Mark directory as > Source Root

Then unchecking "Add content roots to PYTHONPATH" and checking "Add source root to PYTHONPATH" in the Run/Debug config menu. I then checked the folder pathing by doing:

import sys
logger.info(sys.path)

This outputed:

[    '/usr/local/my_project_root/my_sources_root',     '/usr/local/my_project_root/my_sources_root',     '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu',     '/usr/lib/python3.4/lib-dynload',     '/usr/local/lib/python3.4/dist-packages',     '/usr/lib/python3/dist-packages']

However, without the fix it said:

[    '/usr/local/my_project_root/my_sources_root',     '/usr/local/my_project_root',                   <-- NOT WANTED    '/usr/lib/python3.4',     '/usr/lib/python3.4/plat-x86_64-linux-gnu',     '/usr/lib/python3.4/lib-dynload',     '/usr/local/lib/python3.4/dist-packages',     '/usr/lib/python3/dist-packages']

Which meant I got the project root folder included. This messed up the pathing for me.

Solution 4 - Intellij Idea

This was done with PyCharm Community 2019.1

  1. Go to Project Settings Settings
  2. Go to Project Structure and right click on the directory you want to add and click "Sources" Project Structure - add sources

This should add the directory to your pythonpath Project Structure - after add sources

Solution 5 - Intellij Idea

An update to the correct answer phil provided, for more recent versions of Pycharm (e.g. 2019.2).

Go to File > Settings and find your project, then select Project Interpreter. Now click the button with a cog to the right of the selected project interpreter (used to be a ...).

enter image description here

From the drop-down menu select Show All... and in the dialog that opens click the icon with a folder and two sub-folders.

enter image description here

You are presented with a dialog with the current interpreter paths, click on + to add one more.

Solution 6 - Intellij Idea

In my experience, using a PYTHONPATH variable at all is usually the wrong approach, because it does not play nicely with VENV on windows. PYTHON on loading will prepare the path by prepending PYTHONPATH to the path, which can result in your carefully prepared Venv preferentially fetching global site packages.

Instead of using PYTHON path, include a pythonpath.pth file in the relevant site-packages directory (although beware custom pythons occasionally look for them in different locations, e.g. enthought looks in the same directory as python.exe for its .pth files) with each virtual environment. This will act like a PYTHONPATH only it will be specific to the python installation, so you can have a separate one for each python installation/environment. Pycharm integrates strongly with VENV if you just go to yse the VENV's python as your python installation.

See e.g. this SO question for more details on .pth files....

Solution 7 - Intellij Idea

Latest 12/2019 selections for PYTHONPATH for a given interpreter. enter image description here

Solution 8 - Intellij Idea

Well you can do this by going to the interpreter's dialogue box. Click on the interpreter that you are using, and underneath it, you should see two tabs, one called Packages, and the other called Path.

Click on Path, and add your VM path to it.

Solution 9 - Intellij Idea

In pycharm 5 follow this, https://www.jetbrains.com/pycharm/help/configuring-python-interpreter-for-a-project.html

1)Open the Settings dialog box, and click Project Interpreter page.
2)In the Projects pane, choose the desired project.
3)For the selected project, choose SDK from the list of available Python interpreters and virtual environments.

Solution 10 - Intellij Idea

In Intellij v2017.2 you can go to:

run > edit configurations > click ... next to the field 'Environment variables' > click the green + sign

Name= PYTHONPATH

value= your_python_path

Solution 11 - Intellij Idea

Pycharm 2020.3.3 CE ZorinOS(Linux) File>Settings > Project Structure > {select the folder} > Mark as Source(blue folder icon) > Apply

To verify:

import sys
print(sys.path)

Selected path should be listed here.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbaalexanderView Question on Stackoverflow
Solution 1 - Intellij IdeaphilView Answer on Stackoverflow
Solution 2 - Intellij IdeaBjornView Answer on Stackoverflow
Solution 3 - Intellij IdeaAndré C. AndersenView Answer on Stackoverflow
Solution 4 - Intellij IdeadustindorrohView Answer on Stackoverflow
Solution 5 - Intellij IdeaFantaView Answer on Stackoverflow
Solution 6 - Intellij Ideaphil_20686View Answer on Stackoverflow
Solution 7 - Intellij IdeaRichard KeeneView Answer on Stackoverflow
Solution 8 - Intellij IdeaGames BrainiacView Answer on Stackoverflow
Solution 9 - Intellij IdeaSaurabhView Answer on Stackoverflow
Solution 10 - Intellij IdeaJoris KinableView Answer on Stackoverflow
Solution 11 - Intellij IdeaNitish KumarView Answer on Stackoverflow