91

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?

1
  • I have exactly this issue, except that I have many projects following a common scheme and right now (in Emacs) I set up everything dynamically. Setting it up on a per-project basis in Pycharm looks like a burden. Commented Feb 3, 2014 at 20:41

11 Answers 11

134

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

Sign up to request clarification or add additional context in comments.

8 Comments

This is what I do in my project, so annoying. I have project A , project common, project test. In order to run unittests under project test, I added project dependencies so project test depends on project A and common, but I still have to manually add folders from A and common into the PYTHONPATH.
This will add the directory to the global PYTHONPATH for that interpreter right? Not just the project?
@Ray yes, seems it will change the global interpreter settings. But we can use different virtualenv for different projects and set different PYTHONPATH for them.
Note that you have to first set the the interpreter as your project's interpreter, otherwise, the leftmost button is disabled
On OSX, Cmd+Shift+Period shows hidden folders in the select dialog (e.g. /usr) - since there doesn't seem to be a way to just type the path.
|
84

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

7 Comments

Does this still apply? I supposedly have the latest PyCharm professional but I am missing a lot of those categories.. . including Project Structure! But thank you for posting this!!!
Hi, I have updated the screenshot for PyCharm 4.5, the most recent version. It looks like they reorganized the settings in the sidebar.
Actually I owe you a MASSIVE thank you. I was messing with the "default settings" instead of the project settings. Your answer here helped me realize this and then i resolved many discrepancies and it worked. THANK YOU!
Is there a way to do this globally? adding /Library/Python/2.7/site-packages and /usr/local/lib/python2.7/site-packages to every project I open stinks.
Yes @jeremyjjbrown, I believe you can do that by clicking on "Project Interpreter" and selecting the interpreter path that has the site packages you want. This question was just answering making project specific packages available to their projects. You can even use a requirements.txt to add all your deps project in pycharm.
|
16

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.

4 Comments

Thank you, I'm no Python expert and alwasy wondered how to peek at what import paths I can reach! (that sys.path part)
This is not ideal because requires updating every run configuration.
@javadba If you want it to work for all configurations go to Run > Edit Configuration > Templates > Python instead.
@AndréC.Andersen thx! I never even looked at Templates before.
15

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

Comments

6

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.

1 Comment

THANK YOU!!! A colleague and I have been battling with getting his environment set up to match mine, and I have no recollection of doing these steps, but there's an important path in there that I have (which even says "added by user").
3

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....

Comments

1

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

Comments

0

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.

2 Comments

When I click the remote interpreter and add a Classpath, it complains when I specify a remote directory (like /home/vagrant/mydir). Do I need to preface the path with something? My interface is a little different since I'm using IntelliJ with the Python plugin and not PyCharm if that helps.
Well you can just go to SSH interpreter, and load the stuff from vagrant file, its an option there.
0

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.

Comments

0

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

Comments

0

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.

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.