4.8 KiB
Pyenv and Poetry
Pyenv is meant to manage multiple Python versions installed in the computer. It allows you to download different Python versions. You can set a global Python version and a different local Python version for a specific project, so when you enter that directory, the Python version is automatically switched to the one you set.
Poetry is meant to manage virtual environment of a Python project. Poetry does not have a way to download Python specific version needed in a project, so it requires Pyenv to provide one.
Typical Workflow
You need to replace <project name> and <Python version> based on your needs. For example, you may set <project name> to my-project and <Python version> to 3.11.3.
New Project
-
Decide which Python version should be used for the new project.
-
Ensure you have the Python version needed in the computer. You may check it through
pyenv versionsIf it is not installed yet, you may run
pyenv install <Python version>in the terminal.!!! note
You can also check installable Python list by running `pyenv install --list` -
Create the new project folder by running
poetry new --src <project name>and change directory to the project foldercd <project name> -
Now set the Python version locally
pyenv local <Python version> -
Ensure Python version requirement in
pyproject.tomlsatisfies Python version specified in the previous step -
Set Poetry to create the virtual environment and prefer active Python in the path
poetry config virtualenvs.in-project true --local poetry config virtualenvs.prefer-active-python true --local!!! note
`in-project` is needed to ensure the virtual environment folder `.venv` is created in the project root folder. This is useful when you want find this folder easily during docker multi-stage build. `prefer-active-python` is needed to ensure Poetry uses the Python version specified in the previous step. You can also set the configuration globally by replacing `--local` with `--global`. This is useful when you want to use the same configuration for all projects. -
Create the Python virtual environment by running
poetry install -
Activate the virtual environment by running
poetry shell -
You can specify third-party dependencies with
poetry add(doc here) in the terminal or directly editpyproject.toml
Convert Existing Project
!!! tip
It is usually unnecessary to convert existing Python virtual environment manager to Poetry if you already have one. However, if you want to convert it, you can follow the steps below.
It follows the same steps above, except in creating project folder since you already existing one. So instead of running poetry new --src <project name>, run poetry init --no-interaction --python <Python version>.
-
References:
Conda or Mamba
Typical Workflow
You need to replace <project name>, <Python version>, and <environment name> based on your needs. For example, you may set <project name> to my-project, <Python version> to 3.11.3, and <environment name> to my-env.
If mamba is installed, replace conda command with mamba for faster third-party packages installation.
New Project
-
Create new folder
mkdir <project name> && cd <project name> -
Specify the Python virtual environment requirements by creating
environment.ymlfile in project root foldername: <environment name> channels: - conda-forge dependencies: - python=<Python version> - pandas=2.0 - ... -
Create Python virtual environment and install third party dependencies by running
conda env create -f environment.yml -
Activate the environment by running
conda activate <environment name> -
You can specify third-party dependencies with
conda install(doc here) in the terminal or directly editenvironment.yml
-
References:
Convert Existing Project
!!! tip
It is usually unnecessary to convert existing Python virtual environment manager to Conda if you already have one. However, if you want to convert it, you can follow the steps below.
It follows the same steps above, except in creating project folder since you already existing one. You may freeze the existing dependencies through pip freeze > requirements.txt and create environment.yml file with the following content:
name: <environment name>
channels:
- conda-forge
dependencies:
- python=<Python version>
- pip
- pip:
- -r requirements.txt