Guide to Managing Python Environments with pyenv and Poetry
Introduction
pyenv is a tool for managing multiple Python versions on the same system, while Poetry is a modern tool for dependency management and project packaging. This combination helps developers control exact Python versions, automate virtual environment creation, and ensure consistent libraries across different machines.Detail
First, use the following commands to install and use pyenv and poetrybrew install pyenv poetry
pyenv install 3.11.9
pyenv global 3.11.9
pyenv local 3.12.0
poetry env use python
poetry config virtualenvs.in-project true
poetry init -n
poetry add requests
poetry install
- brew install pyenv poetry: Install pyenv and poetry tools via Homebrew.
- pyenv install 3.11.9: Install Python version 3.11.9 to the system, you could install multiple Python version with the same way.
- pyenv global 3.11.9: Set Python 3.11.9 as the system-wide default version.
- pyenv local 3.12.0: Set Python 3.12.0 as the specific version for the current directory.
- poetry env use python: Specify the current Python version for Poetry to create a virtual environment.
- poetry config virtualenvs.in-project true: Configure Poetry to create the virtual environment (.venv) inside the project directory.
- poetry init -n: Quickly initialize a new pyproject.toml file with default settings.
- poetry add requests: Install the requests library and automatically add it to the dependencies list.
- poetry install: Install all libraries declared in the pyproject.toml file.
The pyproject.toml file will be created automatically, after adding packages, they will appear in the dependencies section
[project]
name = "project-name"
version = "0.1.0"
description = ""
authors = [
{name = "Name",email = "email@gmail.com"}
]
requires-python = ">=3.11"
dependencies = [
"numpy (>=2.4.4,<3.0.0)",
"requests (>=2.33.1,<3.0.0)"
]
[tool.poetry]
packages = [{include = "python_3"}]
[build-system]
requires = ["poetry-core>=2.0.0,<3.0.0"]
The poetry.lock file is also created automatically every time a package is added. You can see that poetry stores the version and file hash of each dependency, so that when you reinstall later, you will install the correct version of each dependency, avoiding version mismatches
[[package]]
name = "requests"
version = "2.33.1"
description = "Python HTTP for Humans."
optional = false
python-versions = ">=3.10"
groups = ["main"]
files = [
{file = "requests-2.33.1-py3-none-any.whl", hash = "sha256:4e6d1ef462f3626a1f0a0a9c42dd93c63bad33f9f1c1937509b8c5c8718ab56a"},
{file = "requests-2.33.1.tar.gz", hash = "sha256:18817f8c57c6263968bc123d237e3b8b08ac046f5456bd1e307ee8f4250d3517"},
]
Create a main.py file with the following content to use requests. This content is simply a request call with the GET and POST methods, so I won't explain much
import requests
try:
response = requests.get('https://jsonplaceholder.typicode.com/posts/1', timeout=5)
response.raise_for_status()
data = response.json()
print(f"Title: {data['title']}")
except requests.exceptions.HTTPError as err:
print(f"Error HTTP: {err}")
except Exception as err:
print(f"Error: {err}")
payload = {
'title': 'Title value',
'body': 'Body content',
'userId': 1
}
post_response = requests.post(
'https://jsonplaceholder.typicode.com/posts',
json=payload
)
print(f"\nStatus: {post_response.status_code}")
print(f"Response data: {post_response.json()}")
The result is as follows
$ poetry run python main.py
Title: sunt aut facere repellat provident occaecati excepturi optio reprehenderit
Status: 201
Response data: {'title': 'Title value', 'body': 'Body content', 'userId': 1, 'id': 101}
Happy coding!
See more articles here.
See more articles here.
Comments
Post a Comment