AI
Machine Learning
Python and Venv

Setup

Install Anaconda

https://www.anaconda.com/download (opens in a new tab)

Verify your installation with

conda --version

Open Anaconda Navigator and launch Jupyter Notebook or JupyterLab.

Create a virtual environment

Method 1 Conda

cd into your project directory

 
conda create -p venv python=3.8
 
conda activate venv
 

To activate and deactivate this environment, use

 
conda activate venv/  # or cd into the directory and `conda activate`
conda deactivate
 

Method 2 Using Local python

cd into your project directory

python -m venv myenv

activate your venv

 
./myenv/Scripts/activate
 

By default the version of python in your venv is the same as your system's python version

Method 3 Using virtualenv

pip install virtualenv
 
virtualenv - p python3 virtual_env
 
./virtual_env/Scripts/activate
 
 

Check whether your virtual env is active or not.

where python

Requirements.txt file

Create a requirements.txt file in your project directory to manage dependencies.

pip freeze > requirements.txt # create a requirements.txt file

If you want to switch python version within your conda venv, use:

conda install python=3.9

Now to install the dependencies listed in requirements.txt, use:

pip install -r requirements.txt

.ipynb file

Jupyter Notebook files have the .ipynb extension. You can create a new Jupyter Notebook by running:

jupyter notebook

This will open the Jupyter Notebook interface in your web browser, where you can create and edit .ipynb files.

If you want to execute a specific .ipynb file vscode within your venv , use:

pip install ipykernel 
# python -m ipykernel install --user --name=venv