How do you set your pythonpath in an already-created virtualenv?
The most elegant solution to this problem is here.
Original answer remains, but this is a messy solution:
If you want to change the PYTHONPATH
used in a virtualenv, you can add the following line to your virtualenv's bin/activate
file:
export PYTHONPATH="/the/path/you/want"
This way, the new PYTHONPATH
will be set each time you use this virtualenv.
EDIT: (to answer @RamRachum's comment)
To have it restored to its original value on deactivate
, you could add
export OLD_PYTHONPATH="$PYTHONPATH"
before the previously mentioned line, and add the following line to your bin/postdeactivate
script.
export PYTHONPATH="$OLD_PYTHONPATH"
How do I add a path to PYTHONPATH in virtualenv
You can usually avoid having to do anything with PYTHONPATH by using .pth
files. Just put a file with a .pth extension (any basename works) in your virtualenv's site-packages folder, e.g. lib\python2.7\site-packages
, with the absolute path to the directory containing your package as its only contents.
How to add to pythonpath in virtualenvironment
"I can just add to user variables" just means adding an environment variable to the command shell. Virtualenv shouldn't (by default at least) clear environment variables. So it should just work if you already have PYTHONPATH
set.
If you only want the path available to the virtualenv then modify the file called Scripts/activate.bat
in the virtualenv folder and add the line:
set PYTHONPATH=path/to/dir
Why does virtualenv inherit $PYTHONPATH from my shell?
I stumbled onto this answer about $PYTHONPATH which solved this for me just now. Essentially, setting $PYTHONPATH is optional and is a convenience to the user. It should only contain additional paths the user wants to add to their python path so that the user doesn't have to do this in python itself just to run a script from the terminal.
So to solve my problem above, I set my $PYTHONPATH (in my zshrc) to only my additional folder of '$HOME/dev' and nothing else. This eliminated the references to python2 in my path and all my python3 programs are starting as expected in my virtualenv.
How can I set up a virtual environment for Python in Visual Studio Code?
P.S.:
I have been using Visual Studio Code for a while now and found an another way to show virtual environments in Visual Studio Code.
Go to the parent folder in which
venv
is there through a command prompt.Type
code .
and Enter. [It is working on both Windows and Linux for me.]That should also show the virtual environments present in that folder.
Original Answer
I almost run into same problem every time I am working on Visual Studio Code using venv. I follow the below steps:
Go to menu File → Preferences → Settings.
Click on Workspace settings.
Under Files:Association, in the JSON: Schemas section, you will find Edit in settings.json. Click on that.
Update
"python.pythonPath": "Your_venv_path/bin/python"
under workspace settings.
(For Windows): Update"python.pythonPath": "Your_venv_path/Scripts/python.exe"
under workspace settings.Restart Visual Studio Code in case if it still doesn't show your venv.
Note: Use python.defaultInterpreterPath
instead of python.pythonPath
for newer versions.
how can I find the path of virtualenv python
You can use which
to find out which binary will be executed...
For example:
$ which python3
/home/attie/projects/thing/venv/bin/python3
By default it just shows the first match, but you can give the -a
argument to show all:
$ which -a python3
/home/attie/projects/thing/venv/bin/python3
/usr/bin/python3
Is my virtual environment (python) causing my PYTHONPATH to break?
That's the point of the virtualenv. It doesn't inherit from the rest of your setup. If you want a PYTHONPATH, you need to explicitly set one.
This djangousers post is probably helpful, you want to use virtualenvwrapper to solve this problem.
More info in this other SO post on a similar problem.
Related Topics
Using Pip3: Module "Importlib._Bootstrap" Has No Attribute "Sourcefileloader"
How to Update a Python Package
How to Create a Density Plot in Matplotlib
What Is the 'Self' Parameter in Class Methods
How to Add to the Pythonpath in Windows, So It Finds My Modules/Packages
Why Is the Output of My Function Printing Out "None"
Best Way to Structure a Tkinter Application
Are a Wsgi Server and Http Server Required to Serve a Flask App
How to Protect Python Code from Being Read by Users
Check If a Given Key Already Exists in a Dictionary
How to Change the Environment of a Parent Process in Python
How to Make Python Script Run as Service
Does Python Have a Ternary Conditional Operator
Switch Between Two Frames in Tkinter
How to Parse Xml and Get Instances of a Particular Node Attribute
How to Add Sequential Counter Column on Groups Using Pandas Groupby