跳至主要內容

Setup your development environment for Python via Pyenv and Poetry – Part 3

In part 1 and part 2, we have explained how to install Pyenv and Poetry…

There are still few steps away from our goal.

We can now checkout the current Poetry config by

poetry config --list

The most important part here is virtualenvs.in-project.

It indicates whether or not the virtual environment will be created within your project directory or at the virtualenvs.path directory, which is the default location for Pyenv virtual environments.

For me, personally, I like each project has its own venv and the venv should be in the project. To make that change, you can just easily type

poetry config virtualenvs.in-project true

There are few Poetry commands that I usually use

If you want more details, please refer the official docs

  • poetry init – Initial poetry which will let you create a pyproject.toml for your project.
  • poetry env use python – Create a virtual environment based on your python version.
  • poetry shell – Activate your virtual environment, if it doesn’t exist, then create a new one for you.
  • poetry add {package} {-D} – Add package and its dependencies to your project.
  • poetry remove {package} – Remove pacakge and its dependencies from your project.
  • poetry update – Update your package.
  • poetry lock {--no-update} – Generate/update poetry.lock file.
  • poetry show {--tree} – Show all packages and dependencies in your project.
  • poetry install – Read pyproject.toml file and install all necessary dependencies.
  • poetry check – Validate pyproject.tomlfile
  • poetry export -f requirements.txt --output requirements.txt {--without-hashes} – Export all packages and dependencies to requirements.txt

Poetry init

You can just follow the instruction to set your pyproject.toml.

The pyproject.toml file is what is the most important here. This will orchestrate your project and its dependencies.

Poetry official docs – https://python-poetry.org/docs/basic-usage/

One thing, I think it’s worth to mention is you can separate the dependencies baded on dev or production.


poetry env use python

This command will create a virtual environment for you and use python means using the python version that set in your PATH. If you use this command with Pyenv, then it will create a virtual environment based on pyenv local/pyenv global.

You can see that in chatbot directory, pyenv local is 3.9.15, but pyenv global is 3.8.15. However, when I called, poetry env use python, the environment was created is using Python 3.9.


poetry shell

This command will activate virtual environment.


poetry add {package} {-D}

Add package and dependencies to your project and -D means adding for development.

You can see openai is added to dependencies in pyproject.toml.


poetry remove {package}

Remove package and dependencies to your project.


poetry update

Update dependencies to the lastest version and update poetry.lock file. Will generate poetry.lock file if it doesn’t exist.


poetry lock {–no-update}

This command generates poetry.lock file which locks the dependencies that declare in pyproject.toml.

By default, this will lock all dependencies to the latest available compatible versions. To only refresh the lock file, use the --no-update option. This command is also available as a pre-commit hook. See pre-commit hooks for more information.

Poetry official docs – https://python-poetry.org/docs/cli/#lock

poetry show {–tree}

Display all packages and its dependencies.


poetry install

If you pull a repository which also use poetry from somewhere or you want to move the project to your new machine. It’s pretty simple, just make sure pyproject.tomlis there and type poetry install.


poetry check

Validate pyproject.toml and tell if there is any error.


poetry export -f requirements.txt –output requirements.txt {–without-hashes}

This command will export all packages and dependencies to requirements.txt.

You can see it’s not a typical requirements.txt which has hash value. To exclude hash values when exporting, use the flag --without-hashes.

With the requirements.txt file, you can easily install the project dependencies using pip, even if you are not using Poetry.

Regarding the hash values, you can refer to the official pip documentation. According to the documentation, you can use the --require-hashes option to install packages with hash values via pip install --require-hashes-r requirements.txt. This will ensure that the installed packages match the exact versions and hashes specified in the requirements.txt file.


cat requirements.txt | xargs poetry add

This command can be used to install existing dependencies from requirements.txt directly.


Conclusion

This series has provided a detailed guide on using pyenv and poetry to create custom Python environments for your projects. I hope this memo will be helpful not only to myself, but also to others. Thank you for reading!

分類:Python

搶先發佈留言

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

由 Compete Themes 設計的 Author 佈景主題