Python Virtual Environment
Tutorials
uv
- 官網: uv
- 安裝不需要 root
# Install with curl or wget
curl -LsSf https://astral.sh/uv/install.sh | sh
wget -qO- https://astral.sh/uv/install.sh | sh
# Instal with pip
pip install uv
# To install the latest Python version:
uv python install
# To install a specific Python version:
uv python install 3.12
Pixi
Miniconda
# Install
mkdir -p ~/miniconda3
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm ~/miniconda3/miniconda.sh
# Post-Install
source ~/miniconda3/bin/activate
conda init --all
# Verify
conda list
conda --version
# Optional: Uninstall
conda deactivate
~/miniconda3/uninstall.sh
Conda
# Create a virtual env
conda create -n myproj python=3.11
# Activate the virtual env
conda activate myproj
# Deactivate the virtual env
conda deactivate
Python 3.4+ built-in venv
# Install venv
sudo apt install python3-venv
# Enable venv
mkdir myproject
cd myproject
python -m venv .venv
# Activate the venv
source .venv/bin/activate
# Delete the venv
deactivate
rm -rf .venv
# Change the App directory after activating venv
cd /path/to
mv old new
cd new/.venv/bin
old_path="/path/to/old/.venv"
new_path="/path/to/new/.venv"
find ./ -type f -exec sed -i "s|$old_path|$new_path|g" {} \;
cd /path/to/new
source .venv/bin/activate
virtualenv and virtualenvwrapper
# Installing virtualenv and virtualenvwrapper
sudo pip install virtualenv virtualenvwrapper
# Update the profile ~/.bashrc
# Add the following lines
# Python virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
# Reload the profile
source ~/.bashrc
# Creating python virtual environment
# The py3cv3 is a self-defined name
mkvirtualenv py3cv3 -p python3
# Enter the specified virtual environment
workon py3cv3
# Exit the the specified virtual environment
deactivate
# List all of the environments.
lsvirtualenv
# Remove an environment
rmvirtualenv py3cv3