How to configure custom PYTHONPATH with VM and PyCharm?

↘锁芯ラ 提交于 2019-12-17 04:15:18

问题


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:


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!




回答2:


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

  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

EDIT: I have updated this screen shot for PyCharm 4.5




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




回答4:


This was done with PyCharm Community 2019.1

  1. Go to Project Settings
  2. 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




回答5:


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




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

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.




回答7:


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.




回答8:


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.




回答9:


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



来源:https://stackoverflow.com/questions/17198319/how-to-configure-custom-pythonpath-with-vm-and-pycharm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!