I will leave how to use Poetry as a memorandum.
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python
Here are the commands that you might use personally.
poetry self update
poetry new <project-name>
poetry add <package-name>
poetry remove <package-name>
poetry show
poetry run <commands...>
Run the file with python
poetry run python <file-name>
poetry shell
Launch a shell according to $ SHELL
Immediately after installation, either source
to pass the path or restart the shell
Allows a virtual environment to be created in the project
poetry config virtualenvs.in-project true
This command can be executed once after installation.
Create a sample project
poetry new poetry-sample
Move to project directory
cd poetry-sample
Install the package you want to add
Here, install numpy
poetry add numpy
Create sample.py in the poetry-sample
directory
sample.py
import numpy as np
x = np.array([1, 2, 3])
print(x)
Launch a shell in a virtual environment
poetry shell
Run script
python sample.py
Or you can run commands directly in a virtual environment without launching a shell
poetry run python sample.py
With the above flow, you can create a project, add a package, and execute a program.
The script can be executed even if it is not in poetry-sample
It is a method to do it together with pyenv
Take Python 3.7.0 as an example
First, install 3.7.0 if it is not installed with pyenv
pyenv install 3.7.0
Change the local version in the project
pyenv local 3.7.0
Build a virtual environment with commands
poetry env use 3.7.0
Now the Python version in your project is 3.7.0
It's very convenient, so I'd like to use it from now on.
Recommended Posts