This blog post is a small cheat sheet on setting up a virtual environment for Python venv.
Note: The virtual environment installation.
python3 -m pip install virtualenv --user
Step 1: Create a virtual environment
python3.11 -m venv my-env-3.11
When you look into your current folder, you will notice that the execution of this command created a new folder called my-env-3.11.
my-env-3.11
|-bin
|-include
|-lib
pyvenv.cfg
Step 2: Activate the environment
source ./my-env-3.11/bin/activate
Step 3: Install your project-related requirements
python3 -m pip install -r requirements.txt
Step 3: Deactivate the virtual environment
deactivate
In addition:
Show your current virtual Python environment.
virtualenv --version
Verify and manage your Python versions with pyenv. (blog post “intro to pyenv”)
#install or update
brew update
brew upgrade pyenv
#system
which python
#pyenv
pyenv versions
pyenv -v
pyenv install --list
pyenv install -v 3.11.2
#optional: export PYENV_VERSION=3.11.2
pyenv global 3.11.2
pyenv versions
I hope this was useful to you, and let’s see what’s next?
Greetings,
Thomas
#python, #venv, #cheatsheet
In this context it can be useful to know this:
- Problem: error: externally-managed-environment
error: externally-managed-environment
× This environment is externally managed
╰─> To install Python packages system-wide, try brew install
xyz, where xyz is the package you are trying to
install.
- Solution 1(Linux): Remove `EXTERNALLY-MANAGED`
rm /opt/homebrew/Cellar/python@3.12/3.12.3/EXTERNALLY-MANAGED
- Solution 2 (macOS): Use the –break-system-packages option
python3 -m pip install pandas --break-system-packages
- Solution 3: Using pipx
brew install pipx
sudo pipx ensurepath
