ChatGPT解决这个技术问题 Extra ChatGPT

How to configure custom PYTHONPATH with VM and PyCharm?

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?

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.

p
phil

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!

https://i.stack.imgur.com/wqbAk.png

https://i.stack.imgur.com/gQmnA.png

https://i.stack.imgur.com/SRk8Qm.png


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

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 ⌘,).

https://i.stack.imgur.com/o9f2D.png

Look for Project Structure in the sidebar on the left under Project: Your Project Name Add or remove modules on the right sidebar

https://i.stack.imgur.com/sUjFS.png

EDIT: I have updated this screen shot for PyCharm 4.5


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.
A
André C. Andersen

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.


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

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"

https://i.stack.imgur.com/H6WTZ.jpg


F
Fanta

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

https://i.stack.imgur.com/d5iAy.png

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

https://i.stack.imgur.com/0mR49.png

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


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").
C
Community

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


R
Richard Keene

https://i.stack.imgur.com/hlddk.png


G
Games Brainiac

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.


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

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.


J
Joris Kinable

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


N
Nitish Kumar

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.