Virtual Environment
Contributors: Alanna Zhou, Shungo Najima
You can consult some helpful Python docs for more information!
In the following code blocks, the dollar sign $
just means you should be typing what follows in your terminal/command prompt!
1. Install virtualenv
virtualenv
Navigate to the directory that you want to be coding in, and make sure you have virtualenv
installed
2. Create a venv
folder
venv
folderYou could actually name this anything you want, but it's good practice to call it venv
You create this folder so you can activate later to essentially "enter your virtual environment"
3. Activate the venv
folder
venv
folder You can see if it's been created with ls
for MacOS or dir
for Windows
MacOS command to activate virtual environment:
Windows command to activate virtual environment:
If you are getting an error that reads: "cannot be loaded because the execution of scripts is disabled on this system".
, close PowerShell (if open), look up PowerShell on Windows search, click on "run as administrator", then run the command below:
If you are curious as to what exactly this is doing to your computer, it is allowing the current user to run scripts. RemoteSigned
is one of many execution policies which you can read about here, and CurrentUser
is one of many scopes which you can read about here.
If you're still getting an error, try running these commands on PowerShell instead of Command Prompt.
4. Install requirements.txt
requirements.txt
Now that you're in the environment, your terminal should have a (venv)
next to your cursor, so you can now install the requirements to setup your virtual environment
Make sure requirements.txt
exists where you are running this command! (again, you can check this using ls
/dir
to list everything in your current directory).
If you are curious, -r
is a flag that installs the requirements recursively to install all requirements defined in requirements.txt
.3
5. Run your code!
Now you can run code with the dependencies that were installed by Step 4.
Here's just an example:
6. If you're done, deactivate
If you're done developing for your project for the time being, or if you want to work on another project that has another virtual environment, you can deactivate the one that you're currently in
Common Errors
no such file or directory: requirements.txt
requirements.txt
There are two issues that cause this problem:
You are not in the directory where
requirements.txt
exists. You'll know this is the case if enterls
(MacOS) ordir
(Windows) into your terminal and you don't seerequirements.txt
or if you didn't spell it right in your command.You are trying to do
pip3 install -r requirements.txt
inside of a correctly created and activatedvenv
, but yourvenv
is nested within a path that contains a folder that has a space in the name. Here's a good Stack Overflow post that explains the issue.
Last updated