Virtual Environment Setup
Last updated
Last updated
For the project we will use python virtual environments, this is a common practice for python projects.
Virtual environments are isolated environments with their own dependencies (packages).
Before proceeding make sure:
Open the template (that you have cloned from GitHub) in VS Code.
Open the terminal. (Ctrl+j or Cmd+j)
Execute python --version
If you get a response such as 3.13.0
or similar, you're good to go.
If you get an error (e.g. python is not recognized as a ... command
), this mainly happens on windows, if you're using macOS and this happens, contact a mentor:
Make sure you have installed python and during the installation you check the box on "Add Python [version] to PATH":
If you are sure that you have installed python but you are still encountering this issue, then find where python is installed on your system:
Usually it is under:
C:\Users\[Username]\AppData\Local\Programs\Python\Python[version]
For example: C:\Users\Saman\AppData\Local\Programs\Python\Python313
If it is not there:
In VS Code, open the command pallete by hitting ctrl+shit+p.
Type in: Python: Select Interpreter
. You should now see at least one option, and a path next to it, like so:
The path is then the path to your python installation.
Now that you know the path to your python installation folder, hit the windows key, search 'environment', and open the 'Edit the system environment variables' page:
Then select:
Now, if you are the main and only user of your computer, take a look at the bottom section (under 'system variables'), otherwise look at the top section (under 'user variables for...').
Find the entry named 'path' and double-click it, a new window should open:
In this new window, click the 'New' button, a new empty entry will be added. Here paste the path to your python installation, it should look something like this:
C:\Users\[Username]\AppData\Local\Programs\Python\Python[version]
Click Okay on all windows until they are all closed.
Close VS Code and open it again, then try running python --version
in the terminal again.
If this doesn't work, contact a mentor.
With your project open in VS Code, if the project is open, its path should be displayed in the terminal and the project (folder) name should be displayed at the top of the window, for example:
Now, execute python -m venv venv_name
and replace venv_name
with what you want your virtual envrionment to be called, lacrime_venv
is good for example.
This will create a folder in your project with the same name you just chose, this folder contains:
A new, isolated python installation.
All packages you install in the virtual environment.
Some configuration files etc.
Now we want to let your terminal know you want to use this virtual environment instead of the global python installed on your system. So that when installing packages, you only install them for the project, and not for the entire system. To achieve this, you must activate the virtual enviornment, this is done by running the activate script included in the venv folder.
On windows run: [venv_name]\Scripts\activate
(replace [venv_name]
with what you named your virtual env). This will most likely cause an error, if so, see the next step.
On mac run: source [venv_name]/bin/activate
(replace [venv_name]
with what you named your virtual env)
[Only for windows] if the last step causes an error like ...cannot be loaded because the execution of scripts is disabled on this system.
then:
Hit the windows key and search 'powershell', there should be an option 'windows powershell', right-click it, and choose 'run as administrator'.
A black or blue terminal should open, here execute Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
, Then you will be asked something similar to Yes[Y]/All[A]/No[N]..
here type in A
and hit enter.
After this step, close VS Code and open it again and try step 4 again, if this still doesn't work, contact a mentor.
Your terminal should look a bit different now, the venv name should show up before the command line:
You can install packages in your venv (virtual environemnt), for pandas for example: pip install pandas
Now we will let jupyter notebook know you want to run your notebooks using the python interpreter present in your virtual environment (which only uses the packages you install in it), to do this:
Open the needed .ipynb (jupyter notebook) file, on the top right you should see a button with 'select kernel' or 'python 9.13.0' or something similar.
Click on it, a little window will open on the top, then click 'python environments':
you should then see a list of python interpreters available on your system, choose the one with the name of your venv on it, the button should now look like this:
you should be good to go! Phew...๐ฅ