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:
/home/tejul/Documents/gr13/default/lib/python2.7
, this did not help$PYTHONPATH
instead of PYTHONPATH
, this did not help.env
file, this did not helpPYTHONPATH="/home/tejul/Documents/gr13/default/lib/python2.7:/usr/lib/python2.7"
, this did not helpOP 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.