How to configure custom PYTHONPATH with VM and PyCharm?
Solution 1
For PyCharm 5 (or 2016.1), you can:
- select Preferences > Project Interpreter
- to the right of interpreter selector there is a "..." button, click it
- select "more..."
- pop up a new "Project Interpreters" window
- select the rightest button (named "show paths for the selected interpreter")
- pop up a "Interpreter Paths" window
- click the "+" buttom > select your desired PYTHONPATH directory (the folder which contains python modules) and click OK
- Done! Enjoy it!
Solution 2
Instructions for editing your PYTHONPATH or fixing import resolution problems for code inspection are as follows:
- Open Preferences (On a Mac the keyboard short cut is
⌘,
).
Look for
Project Structure
in the sidebar on the left underProject: Your Project Name
Add or remove modules on the right sidebar
EDIT: I have updated this screen shot for PyCharm 4.5
Solution 3
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
This was done with PyCharm Community 2019.1
- Go to Project Settings
- Go to Project Structure and right click on the directory you want to add and click "Sources"
This should add the directory to your pythonpath
Solution 5
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 ...).
From the drop-down menu select Show All... and in the dialog that opens click the icon with a folder and two sub-folders.
You are presented with a dialog with the current interpreter paths, click on + to add one more.
baalexander
Updated on February 20, 2021Comments
-
baalexander over 3 years
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?