问题
How do I correctly set up the $PYTHONPATH
variable for my workspace in VisualStudio Code?
Background Information
I have installed two versions of GNURadio:
GNURadio version 3.7.11 installed by the Linux Mint package manager in
/usr/lib/python2.7/dist-packages/gnuradio
GNURadio version 3.7.13.4 installed by PyBOMBS in
/home/tejul/Documents/gr13/default/lib/python2.7/dist-packages/gnuradio
(my prefix directory is~/Documents/gr13/default
)
I can use the newer version of GNURadio version only after I run the setup_env.sh
script (which -- among other things -- adds /home/tejul/Documents/gr13/default/lib/python2.7/dist-packages
to $PYTHONPATH
) and then start python in the terminal
tejul@Wacom:~/Documents/gr13/default$ ls
bin etc include lib libexec setup_env.sh share src
tejul@Wacom:~/Documents/gr13/default$ source ./setup_env.sh
tejul@Wacom:~/Documents/gr13/default$ python
Python 2.7.15rc1 (default, Nov 12 2018, 14:31:15)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from gnuradio import gr
>>> gr.version()
'3.7.13.4'
>>>
Without modifying the $PYTHONPATH python -- naturally -- imports the older version of GNURadio.
I want to write, run, and debug python scripts for the new version of GNURadio in the VisualStudio Code. I've been trying to understand the selection of python interpreters, workspaces, and environments for VSCode.
As far as I understand it, the VSCode workspace setting python.pythonPath
is not to be confused with the environment variable $PYTHONPATH
. python.pythonPath
is the path to the python interpreter used for debugging or running the code, while $PYTHONPATH
is the environment variable which python uses to search for modules.
It looks like PyBOMBS did not install its own python interpreter into my prefix directory. So I need to use VSCode with my normal python interpreter located in /usr/bin/python2.7
. So redefining VSCode's python.pythonPath
or selecting another python interpreter would not help me.
I need to let VSCode use my own version of the environment variable $PYTHONPATH
which would tell my regular python interpreter to import modules preferably from /home/tejul/Documents/gr13/default/lib/python2.7/dist-packages
.
Problem
Following the documentation, I have created my own .env
file in the workspace directory which sets the order of preference for locations from which python should import the modules. Alas, it has no effect on the python interpreter.
Can you see anything that I am doing wrong here? I have also tried:
- Setting the PYTHONPATH to one folder level higher, i.e.
/home/tejul/Documents/gr13/default/lib/python2.7
, this did not help - Calling the variable
$PYTHONPATH
instead ofPYTHONPATH
, this did not help - Restarting VSCode after each change of the
.env
file, this did not help - Using double quotes around the path string, e.g.
PYTHONPATH="/home/tejul/Documents/gr13/default/lib/python2.7:/usr/lib/python2.7"
, this did not help
回答1:
OP seemed to have asked about path syntax for the .env file and the vscode set up so that it finds and reads some custom module files. My problem was similar in that I wanted code to find my custom modules for import in a script. I did not want to put my custom modules in a folder inside my python environment. I also wanted to avoid setting one or more paths as PYTHONPATH for the User Variables in the Windows Environment Variables - but this will work if you want to do it. I am working in vscode in Windows 10.
1) SYNTAX:
a) I found that the following path syntax works in the env file:
PYTHONPATH = C:/0APPS/PYTHON/_MODULES
My .py module files are in this folder.
b) # works for comments in the .env file.
2) VSCODE SET-UP: I found that the following works:
a) Like sunew said at #2 My setup: Use the Explorer in vscode to open at your selected project workspace folder. For me that is Q:\420 PYTHON
b) Give the env file a name, like vscode.env file and place it in that folder at the top level of the workspace.
c) Open vscode settings and search .env where under the Extensions > Python you will find "Python: env file". Edit the box to add your env file name just before .env. e.g. ${workspaceFolder}/vscode.env
d) import custom_modulename now work for me - in the python interactive window and in a script.
回答2:
Setting PYTHONPATH in .env works for me. Note that the effect just is for vscode and the tools it runs, such as pylint.
My situation: I have a project that is not a pip installable package, but simply a source folder. (For historical reasons...)
myproject/src
The project has dependencies defined in a pip requires file.
My setup:
- I create a virtualenv, and install the packages from the requires file.
- I open vscode in the folder
myproject
- so this becomes the root of the vscode "project". - I point vscode to use the virtualenv as the python interpreter. This will make imports of dependencies installed with pip work. (For linters, intellisense, etc)
- To also make imports from my project source work for the linters (pylint especially) in vscode, I add a .env with this content, adding my project source folder to the PYTHONPATH:
PYTHONPATH=./src:${PYTHONPATH}
回答3:
This question deserves an upvote because the documentation is missing some important details.
Example
Suppose your project layout is like this
myproject/
.vscode/
settings.json
.env
src/
a_module.py
tests/
test_a.py
Open the settings.json fie and insert these lines
"terminal.integrated.env.windows": {
"PYTHONPATH": "${workspaceFolder}/src;${workspaceFolder}/tests"
},
"python.envFile": "${workspaceFolder}/.env",
Note that ${workspaceFolder}
evaluates to myproject/
, it is not to the .vscode
folder.
In the .env file enter this
WORKSPACE_FOLDER=C:/full/path/to/myproject
PYTHONPATH=${WORKSPACE_FOLDER}/src;${WORKSPACE_FOLDER}/tests
Note that on Windows the slashes in the path lean forward, like so /
. Different paths are separated with a ;
(on other platforms with a :
).
This blog was helpful.
来源:https://stackoverflow.com/questions/53653083/how-to-correctly-set-pythonpath-for-visual-studio-code