Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't "activate" virtualenv

New to running Python in virtual environments, messing with Django, and can't activate a virtual environment.

Spent the last 4 hrs trying to activate a virtual env (venv) on local terminal/VS Code with no luck.

Avoided "sudo pip install virtualenv" as I was trying to avoid installing as root and having different directory path, etc.

"pip install virtualenv" output:

Collecting virtualenv Using cached virtualenv-20.0.31-py2.py3-none-any.whl (4.9 MB)
Requirement already satisfied: six<2,>=1.9.0 in /Users/user/Library/Python/3.8/lib/python/site-packages (from virtualenv) (1.15.0)
Requirement already satisfied: appdirs<2,>=1.4.3 in /Users/user/Library/Python/3.8/lib/python/site-packages (from virtualenv) (1.4.4)
Requirement already satisfied: filelock<4,>=3.0.0 in /Users/user/Library/Python/3.8/lib/python/site-packages (from virtualenv) (3.0.12)
Requirement already satisfied: distlib<1,>=0.3.1 in /Users/user/Library/Python/3.8/lib/python/site-packages (from virtualenv) (0.3.1)
Installing collected packages: virtualenv
Successfully installed virtualenv-20.0.31

"virtualenv venv" output:

created virtual environment CPython3.8.5.final.0-64 in 416ms
creator CPython3Posix(dest=/Users/user/Desktop/rp-portfolio/distribution/venv, clear=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/user/Library/Application Support/virtualenv)
added seed packages: pip==20.2.2, setuptools==49.6.0, wheel==0.35.1
activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator

"source venv/bin/activate" returns nothing

"./venv/bin/activate" output:

zsh: permission denied: ./venv/bin/activate

"sudo ./venv/bin/activate" output:

sudo: ./venv/bin/activate: command not found

Thoughts?

like image 413
garp Avatar asked Aug 25 '26 14:08

garp


2 Answers

There's a lot of confusing information out there on virtual environments, because of how they have evolved. Since Python 3.3, the venv module is available with Python as part of the standard library to create virtual environments, and if you're just getting started, I'd recommend learning it first. There's nothing extra to install after you've installed Python 3.8.

From your project's home directory in the VSCode terminal, try this:

python3 -m venv venv
. venv/bin/activate
pip install Django

Here's what the three lines do:

  1. Call the Python module venv and create a new virtual environment in the directory venv
  2. Run the script to activate the virtual environment that is located in the path venv/bin/activate
  3. Now that the venv is activated, install Django.

After the first time install, you'll just need to repeat step (2) to activate it. You can also point VSCode to automatically start it when you fire up the IDE. You can click the bar at the bottom of VSCode after installing the Python plugin to select the Python version in the venv you've created. Good luck!

Update:

Here's an example of it working in zsh on my machine:

$ zsh
% python3 --version
Python 3.8.2
% python3 -m venv venv
% . venv/bin/activate
(venv) % pip install Django
Collecting Django
Collecting pytz (from Django)
Collecting asgiref~=3.2.10 (from Django)
Collecting sqlparse>=0.2.2 (from Django)
Installing collected packages: pytz, asgiref, sqlparse, Django
Successfully installed Django-3.1.1 asgiref-3.2.10 pytz-2020.1 sqlparse-0.3.1
like image 65
FlipperPA Avatar answered Aug 27 '26 03:08

FlipperPA


I was stuck on this for a good while but you can try venv:

python -m venv virtualenvname

#to activate the virtual environment

source virtualenvname/Scripts/activate
like image 22
Sadia Anjum Avatar answered Aug 27 '26 04:08

Sadia Anjum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!