In this post we will look at how to set up a virtual environment in python.
The folder that we will use will be a project folder for a upcoming project we can build and use the virtual environment to contain the libraries we will need to make it work.
You can create a directory for this project any where you like but for this example we can use the home directory.
Start with creating a folder. Open the terminal and from the home directory enter the command:
1
mkdir 'name_your_folder'
After the folder in made you will change directory into the folder. Use the following command.
1
cd name_of_your_folder
Once you are in the directory of the folder you created run this command.
1
python3 -m venv venv
Also a specific version can be set for the venv. This can be helpful for working with specific versions of python when needed for comapatability.
Version in homebrews path.
1
/opt/homebrew/bin/python3 -m venv venv
For a specific version in homebrew.
1
/opt/homebrew/opt/python@3.12/bin/python3.12 -m venv venv
Now you can activate the environment.
1
source venv/bin/activate
