Open a Terminal
- Use a keyboard shortcut, typically
Ctrl + Alt + T
.
- Alternatively, you can search for “Terminal” in your system’s application menu and open it from there.
Ensure Python is Installed
You can type the following command into the terminal and ensure the output lists a Python version of 3 or above.
python3 --version
Create a Virtual Environment
Run the following command, which will great a Python virtual environment with the name “env” in your current directory.
Note: You can call this environment whatever you want, but common convention is “venv” or “env”.
python -m venv env
Activate the Virtual Environment
Run the following command to activate the virtual environment. This will mean that your current terminal will be running within the Python virtual environment and no longer using your default Python installation.
Note: You’ll notice the name of the virtual environment appear in brackets before your current directory in the terminal, this is an easy way to know if you’re using the virtual environment or not.
source env/bin/activate
Install Requirements
Given that we’ve just created a new Python environment we don’t have any of our previously installed Python packages as they were installed on a different Python environment. This means we need to reinstall our necessary packages again.
If you have a requirements.txt file in your current directory you can simply do the following:
pip install -r requirements.txt
Alternatively you can install your packages separately, here’s how we’d install the requests module:
pip install requests
Open Command Prompt
- Press
Win + R
to open the Run dialog, type cmd
, and press Enter.
- Or, search for “Command Prompt” in the Start menu and select the application to open it.
Ensure Python is Installed
You can type the following command into the terminal and ensure the output lists a Python version of 3 or above.
py --version
Create a Virtual Environment
Run the following command, which will great a Python virtual environment with the name “env” in your current directory.
Note: You can call this environment whatever you want, but common convention is “venv” or “env”.
py -m venv env
Activate the Virtual Environment
Run the following command to activate the virtual environment. This will mean that your current terminal will be running within the Python virtual environment and no longer using your default Python installation.
Note: You’ll notice the name of the virtual environment appear in brackets before your current directory in the terminal, this is an easy way to know if you’re using the virtual environment or not.
env\Scripts\activate
Install Requirements
Given that we’ve just created a new Python environment we don’t have any of our previously installed Python packages as they were installed on a different Python environment. This means we need to reinstall our necessary packages again.
If you have a requirements.txt file in your current directory you can simply do the following:
pip install -r requirements.txt
Alternatively you can install your packages separately, here’s how we’d install the requests module:
pip install requests