90

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?

baalexander
  • 2,579
  • 1
  • 25
  • 32
  • 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. – AdamC Feb 03 '14 at 20:41

11 Answers11

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

phil
  • 2,558
  • 1
  • 19
  • 23
  • 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. – thinkanotherone Mar 31 '16 at 05:57
  • This will add the directory to the global PYTHONPATH for that interpreter right? Not just the project? – Ray May 06 '16 at 15:48
  • @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. – phil May 07 '16 at 16:59
  • Note that you have to first set the the interpreter as your project's interpreter, otherwise, the leftmost button is disabled – ospider May 21 '17 at 03:27
  • 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. – Flash Oct 19 '17 at 07:53
  • @Flash try cmd+shift+g to type the path – phil Oct 19 '17 at 13:40
  • Could we please get an update for PyCharm 2017 and after? Thanks. – polarise Aug 22 '18 at 09:23
  • I got the equivalent of this to work in PyCharm 2018.1. @polarise -- Ask a new question. – battey Apr 03 '19 at 18:49
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

Bjorn
  • 69,215
  • 39
  • 136
  • 164
  • 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!!! – R Claven Jul 21 '15 at 06:56
  • 1
    Hi, I have updated the screenshot for PyCharm 4.5, the most recent version. It looks like they reorganized the settings in the sidebar. – Bjorn Jul 21 '15 at 20:05
  • 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! – R Claven Jul 23 '15 at 18:58
  • 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. – jeremyjjbrown Oct 28 '15 at 12:28
  • 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. – Bjorn Oct 28 '15 at 16:44
  • @Bjorn Tipling - Close, but that's not so useful if I have local libs I use that I also edit. – jeremyjjbrown Nov 21 '15 at 22:40
  • @jeremyjjbrown sounds like you're not doing something in the conventional way. You probably want virtualenv and a requirements.txt both of which PyCharm supports. – Bjorn Nov 22 '15 at 19:18
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.

André C. Andersen
  • 8,955
  • 3
  • 53
  • 79
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

dustindorroh
  • 311
  • 2
  • 6
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.

Fanta
  • 2,825
  • 5
  • 26
  • 35
  • 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"). – Drarok Sep 06 '19 at 14:07
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....

Community
  • 1
  • 1
phil_20686
  • 4,000
  • 21
  • 38
1

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

Richard Keene
  • 398
  • 3
  • 14
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.

Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
  • 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. – baalexander Jun 20 '13 at 18:00
  • 1
    Well you can just go to SSH interpreter, and load the stuff from vagrant file, its an option there. – Games Brainiac Jun 20 '13 at 20:50
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.

Saurabh
  • 7,525
  • 4
  • 45
  • 46
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

Joris Kinable
  • 2,232
  • 18
  • 29
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.

Nitish Kumar
  • 121
  • 1
  • 6