Python venv things

Conda/miniforge’s base environment

How to have a (base) before a command line like this?

1. After installation: cd to the bin folder, for example:

cd /opt/homebrew/Caskroom/miniforge/base/bin

2. Run init:

./conda init zsh

Exit and reopen the terminal.

Advanced for Mac users

“./conda init zsh” will automatically change the file ~/.zshrc, and help set the PATH environment variable for you.

You could check the file .zshrc before and after by using these commands:

cd ~
cat .zshrc

Venv for project

Creating a new venv

cd to the project folder and run this:

python3 -m venv venvname

or this (you will have to run “pip install virtualenv” first if you have not installed it yet)

virtualenv venvname

Activating a venv

. venvname/bin/activate

or

source venvname/bin/activate

Deactivating an active venv

cd venvname/bin/
$source deactivate

Venv for projects

We could create a venv for multiple projects with the help of conda/miniforge:

0. list all conda discoverable environments:

conda info --envs

If you’re using VSCode or Jupyter Notebook, you could see a list of available environments like this:

1. Creating a new venv:

conda create -n venvname python=x.y.z

x.y.z: python version, you could see a list of available python versions by using:

conda search "^python$"

2. Activating a venv:

conda activate venvname

3. Install a new package:

conda install <package>

For more information see:

conda install --h

4. Deactivating an active venv

conda deactivate

5. Deleting a venv

conda remove -n venvname --all

A note

Note that we could use venv for multiple projects like the way we set up in “2. venv for project”, and this is not smart because we have to remember venv’s path, the venv will also not be included in VSCode or Jupyter Notebook’s environment list: