Python venv management
Updated 2023-09-09: I now revert back to plain requirements.txt
, since pipenv is very poor at resolving large dependencies list. Poetry still remains funky so that's off the table as well.
Updated 2024-01-20: I've been using poetry in production for a few months now and so far it works great! Looks like they ironed out a lot of rough edges. Although I would still recommend people to use pre-commit to generate plain requirements.txt
, this is so that in ci or dockerfile you don't have to install poetry and set in-project venv every time.
When you create a project in python, you should create requirements.txt
to specify dependencies, so other people can have the same environment when using your project.
However, if you don’t specify module versions in requirements.txt
, you could end up with people using the wrong module version, where some APIs can be deprecated or have different behaviors than older versions.
Another issue is that maybe you’re working on a few python projects, each uses different python versions (eg. projectA uses python3.6, projectB uses python3.9, etc).
Enter pyenv
and poetry
(pipenv
is considered outdated now), where you can easily switch python versions, and have different environment (with python version locking) for projects you’re working on.
Setup pyenv
Follow instructions here. For windows, use this.
Useful commands
# list available python versions
# install specific version
# list installed versions
# activate new env
# config venv
# set env per folder/project
Setup pipenv
2024-01-20: See below for poetry, this only stays for reference only
Notes: make sure pyenv
is installed, and remove anaconda / miconda
& python3
installed via official installer from your system. Then run:
# run this command every time pip installs a .exe
pipenv workflow
# install a specific module
# install from existing requirements.txt or from Pipfile definition
# remove venv
# running inside venv
Windows only
2024-01-20: See below for poetry, this only stays for reference only
Setup poetry
Useful commands
# init project
# add deps
# add dev dependencies
# activate venv
# specify python version
Notes
- On linux/mac, do not use system python. OS updates would mean python version upgrade, in turn making all your installed modules gone. Use python installed via pyenv instead.
- On windows, start fresh with pyenv.
- Do not use anaconda distribution. It does too much background magic that can make things harder to manage environment properly. In addition, venv definition from anaconda is often doesn’t work cross-platform (eg. venv def from windows wouldn’t work on mac due to different wheel binary versions).
- Always create venv via pipenv per each project. Although you can have a playground venv via pyenv, so you can shell into it and do a quick analysis / scripting on an adhoc basis.
I heard good things about poetry but it doesn’t integrate withpyenv
natively. It would work if you use it to publish python modules, since it simplifies a lot of processes.poetry also picks up the wrong python version from pyenv. And if you sync python version via pyenv, it has to be the same python version across all OSes, including minor version. pipenv doesn’t have this restriction, and it also picks up the correct python version from pyenv by default (viapipenv --python 3.8
).