Experimental but very promising pip
replacement that handles package managing as well as virtual environments and Python version management.
uv
comes included with uvx
, an alias for uv tool run
. uvx
allos you to install and execute command-line tools on an ephemeral environment.
Note that you don't have to actively install a Python version! uv
will automatically fetch the required Python version for your project.
- Install a Python version (or many)
uv python install 3.11 3.12
- View your installed Python versions
uv python list
A project is a folder with some preconfigured files for ease of use, the most important being pyproject.toml
, which defines the project's dependencies.
- Create a project
uv init my_project
- This will create a
my_project
folder in the current directory with the following files:.python-version
README.md
hello.py
pyproject.toml
- The project folder will NOT contain a virtual environment by default.
- Add packages
uv add package
- You need to execute this from the project's folder.
- If no environment is present,
uv
will automatically create it.
For when you want to create an environment on an already existing project folder.
- Create an environment
uv venv
> creates an environment at.venv
.uv venv name
> creates an environment atname
.uv venv --python 3.11
> creates an environment with the specified Python version.
- Activate the environment
source .venv/bin/activate
> you may change.venv
to whatever folder you used for your venv.
- Deactivate the environment
deactivate
- Install packages from your requirements file (with the venv activated)
uv pip install -r requirements.txt
uv pip install -r pyproject.toml
- Add a single package to the venv (with the venv activated)
uv add package
- Remove a package (with the venv activated)
uv remove package
- If your requirements.txt (or pyproject.toml) file has changed, sync the environment to make sure that all packages match:
uv pip sync requirements.txt
- Run code without activating the venv
uv run python my_app.py
- Export dependencies to
requirements.txt
uv export --format requirements-txt
- Note that the exported file will be a lockfile with pinned versions.
- The actual lockfile used by
uv
isuv.lock
. Bothpyproject.toml
anduv.lock
are more modern solutions thatrequirements.txt
.
- Remove an environment
- Simply remove the .venv folder.
- Clean the cache
uv cache clean
- This will delete the entire cache!
- Clean the cache of unused entries.
uv cache prune
- Much safer than the previous command.
Manages packages and virtual environments for you. Good for Data Science but it sucks for dockerizing.
- Create a virtual environment
conda create --name my_env_name python=3.8
or whatever Python version you may need.
- List available envs (2 different ways
conda env list
conda info --envs
- Activate virtual env
conda activate my_env_name
- Deactivate current environment
conda deactivate
- If pip doesn't work with a fresh conda install:
conda install pip
- Install project dependencies (listed in requirements.txt file)
conda install --file requirements.txt
pip install -r requirements.txt
- Delete an old environment
conda remove --name my_env_name --all
conda env remove -n my_env_name
- Update conda
conda update conda
- Update all packages in the current environment
conda update --all
- Update all packages in another env
conda update -n my_env_name --all
- List installed packages in current environment
conda list
- Add conda-forge channel
conda config --add channels conda-forge
- Check conda channels
conda config --show channels
- Remove conda-forge channel
conda config --remove channels conda-forge
- Create an environment file from your current environment.
conda env export --from-history > environment.yml
- Create a new environment and install dependencies listed in YML file.
conda env create -f environment.yml
- If you don't want the base environment to load automatically whenever you open a new shell, change the configs:
conda config --set auto_activate_base false
- List all available environments
conda env list
- Remove a specific environment
conda env remove --name environment_name_to_be_deleted
- Clean up the cache to remove packages and stuff
conda clean --all
Take a look at Pixi as a drop-in replacement for Conda.
venv
is included with Python. It's clunkier than some other tools but it's available mostly everywhere. It allows you to create virtual environments tied to a specific project because they are contained within a subfolder inside your project's folder.
Keep in mind that virtual environments created with venv
are not activated automatically.
- Create a virtual environment.
python -m venv path/to/virtual/environment/folder
- By convention, the usual virtual environment folder is called
.venv
and placed in the root folder of your project. In these notes, it is assumed that all paths begin from the project's root folder and that.venv
is the chosen name for the virtual environment. - The Python version of the venv will be the same you're using at the moment. I recommend using pyenv for managing your Python versions; you can activate a local Python version on the project folder and generate the venv afterwards.
- Activate the virtual environment.
source .venv/bin/activate
- Install dependencies from a requirements.txt file (you need to be on an activated venv).
pip install -r requirements.txt
- You can use any other
pip
commands, such aspip install package_name
andpip list
to see installed packages.
- Deactivate environment.
deactivate
- Delete the venv folder to get rid of it.
rm -rf .venv
pyenv
is a Python version manager. It allows you to install and manage multiple Python versions.
pipenv
is a Python virtualenv management tools. pipenv
does not have built-in package search; make sure you search for the packages at PyPI.
Be aware that pipenv
has some issues (very slow dependency lock, non-standard config file format) and that there are newer tools that may supplant it, such as PDM or uv.
pyenv
- https://github.com/pyenv/pyenv
pyenv
can be installed with either Brew or with the automatic installer script.- For Windows, there is
pyenv-win
but I have not tested it. - For Ubuntu 22.04 LTS, make sure you run the following before installing:
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm xz-utils tk-dev libffi-dev liblzma-dev python3-openssl
pipenv
- https://pipenv.pypa.io/en/latest/index.html
- Install locally for your user with
pip install pipenv --user
The environments are based on the folder you're on. There is no need to manually name them, and there is no environment activation to take care of per se.
- (Optional) Install the Python version you need
pyenv install 3.11
- Create a new virtual environment with pipenv and choose the Python vrsion you want.
pipenv install --python 3.11
- pipenv should automatically use pyenv to install the specified version if it is not found on your system.
- If a
requirements.txt
file is present, it will be used to install your packages. If aPipfile
is present, it will be used instead ofrequirements.txt
because it has priority.
- If you have a
requirements.txt
and aPipfile
and want to prioritizerequirements.txt
, you can choose which file to use in order to install packages:pipenv install -r requirements.txt
- Install a single package (this will modify
Pipfile
andPipfile.lock
, but WILL NOT modifyrequirements.txt
if it exists):pipenv install some_package
pipenv install some_package=1.0
- If a
Pipfile.lock
file already exists, you can install the packages from it.pipenv sync
- Update packages
pipenv update
> updates all packagespipenv update <package>
updates a single package and its sub-dependencies
- Access the pipenv shell (necessary for enabling the virtualenv and for your script to find the installed packages).
pipenv shell
- Exit a pipenv shell.
exit
- Install all dependencies to the system, avoiding virtualenvs entirely (useful for deployment in containers)
pipenv install --system --deploy
> Use this in your Dockerfile.
Pipenv environments are usually in ~/.local/share/virtualenvs/
; inspect the directory to see all the virtualenvs.
To remove a virtualenv, navigate to its project directory and run:
pipenv --rm
After deleting the environment/s, you may want to clean up the pip cache:
pip cache purge
-
Update apt
sudo apt update && sudo apt upgrade
-
Install pip
sudo apt install python3-pip
-
Install pyenv. Follow instructions here: https://github.com/pyenv/pyenv-installer
-
Install pipenv. Follow instructions here: https://pipenv.pypa.io/en/latest/