Activating a Virtual Environment in the Terminal
In this lesson, you’ll learn how to activate a virtual environment using the activate
script. The lesson also covers one way to check which third party libraries are installed in the virtual environment.
00:00
Now if we run the which pip3
command, you can still see that while we set up the virtual environment, running pip3
would still execute inside the global environment, so what you need to do here is you need to load a script inside the venv/
folder—and again, this is just by convention—where it’s called ./venv/bin/activate
.
00:23
You want to run that inside your shell. On Windows, that would just be a little executable, so you wouldn’t use the source
, you would just kind of run this activate
command. And what you can see now, it actually changed my prompt here, because now this particular venv
is activated and it tells me so by adding this little notice here to my prompt, right?
00:42
And so if I go ahead and run which pip3
again, you can see now that it actually points to a different path inside the virtual environment. And this is great because that means now when I install stuff, it will go inside the virtual environment rather than the global environment, right? So, I can also use the pip3 list
command and that’s going to show me what’s installed here. And you can see here—this is just very basic stuff, right?
01:08
So this is a very basic Python install with the standard library and then pip
and setuptools
, which again are now part of the standard library.
Dan Bader RP Team on March 7, 2020
On Windows, you can use the where.exe
command. This should work:
C:\> where pip
Martie on May 15, 2020
As I’m watching this course I’m struggling with a real venv issue.
I’m trying to set up a python 3.7.3 virtual environment on a web host. I’ve got a subdomain portfolio.pym-ltd.com which I have all my scripts in and I need it to have Python 3.7.3 running.
In the terminal I can do this:
[pymlrfvr@server254 portfolio.pym-ltd.com]$
[pymlrfvr@server254 portfolio.pym-ltd.com]$ python -V
Python 2.6.6
[pymlrfvr@server254 portfolio.pym-ltd.com]$ source /home/pymlrfvr/virtualenv/portfolio.pym-ltd.com/3.7/bin/activate && cd /home/pymlrfvr/portfolio.pym-ltd.com
(portfolio.pym-ltd.com:3.7)[pymlrfvr@server254 portfolio.pym-ltd.com]$ python -V
Python 3.7.3
(portfolio.pym-ltd.com:3.7)[pymlrfvr@server254 portfolio.pym-ltd.com]$
which I thought means I’m running Python 3.7.3 in the subdomain space, BUT when I run a small python script to show the sys.version_info I get:
www.portfolio.pym-ltd.com/ignore%20index.php
This is Python! Honestly! (2, 6, 6, 'final', 0)
Have you any idea why the terminal shows Python 3.7.3, but the code running in the browser identifies 2.6.6.
I can only think I’ve missed something really simple.
markmatthewsdeveloper on July 22, 2022
If your on Windows to activate your environment use this:
.\venv\Scripts\activate
Martin Breuss RP Team on July 25, 2022
Thanks @markmatthewsdeveloper! If someone watching this is on Windows, also check out the associated tutorial that goes into more detail and has all the Windows commands and file structures as well.
@Martie just seeing your comment here so this reply is probably too late for you, but maybe someone else might run into a similar issue. Probably what happened here is that you’re running the script without using the absolute path to your Python executable inside your venv. Check out the linked section for more info. :)
Become a Member to join the conversation.
hirejorge on March 6, 2020
which pip3 command does not work inside of windows vscode. what would be the equivalent in windows?