The original article is here
It's about Python package management and tools for managing virtual environments.
I use Pipenv normally, but I still used poetry properly. I haven't done so, so I gave it a try.
Create project folders for 3.7 and 2.7 with poetry, run poetry run python --version
in each folder, and if the displayed Python version is different, it will be successful.
Recommended in Official poetry installation method
curl -sSL https://raw.githubusercontent.com/sdispater/poetry/master/get-poetry.py | python
With this method, there are the following problems in Windows.
python
hits a mysterious python.exe that is neither Python 2.7 nor 3.7.--preview
option to install 1.0.0bxx, but there seems to be no way to add the option this way.First of all, regarding the mysterious python.exe
problem, this is due to the ** app execution alias ** that seems to have been introduced relatively recently in Windows 10. When you open Windows Settings-> Apps and Features-> App Execution Alias, you will find ** App Installer (python.exe) ** and ** App Installer (python3.exe) **, so turn them off. (Although you can change the priority of the paths)
Regarding the second point, there is no choice but to change to another installation method. Although it is deprecated in the documentation, it will be installed user-globally with the pip command, just like Pipenv. Let's wait for the recommended method to install it nicely. With this method, you need to manually set the path to the folder containing the two files poetry and poetry.bat (this time it was installed in Python 3.7, so % APPDATA% \ Python \ Python37 \ Scripts
).
py -3 -m pip install --user --pre poetry
in a shell such as Powershell% APPDATA% \ Python \ Python37 \ Scripts
to the environment variable PATHpoetry --version
on the shell, this time it will return Poetry version 1.0.0b8
This completes the poetry installation.
poetry also manages the virtual environment (virtualenv), but by default the location of the virtual environment is % USERDIR% \ Local \ pypoetry \ Cache \ virtualenvs
. Since it is easier to create a virtual environment in the project folder for use with Visual Studio Code etc., I will change some poetry settings.
> poetry config --list
cache-dir = "C:\\Users\\xxxxxxxx\\AppData\\Local\\pypoetry\\Cache"
virtualenvs.create = true
virtualenvs.in-project = false (← I want to make this true)
virtualenvs.path = "{cache-dir}\\virtualenvs"
> poetry config virtualenvs.in-project true
> poetry config --list
cache-dir = "C:\\Users\\xxxxxxxx\\AppData\\Local\\pypoetry\\Cache"
virtualenvs.create = true
virtualenvs.in-project = true
virtualenvs.path = "{cache-dir}\\virtualenvs"
If you let poetry create a virtual environment in this state, it will be created in [project folder] \ .venv
.
Now let's create one Python 3.7 and one 2.7 project using poetry. Since poetry is installed on Python 3.7 this time, a project for Python 3.7 will be created unless otherwise specified.
> poetry new py3test
Created package py3test in py3test
> cd py3test
> poetry install
Creating virtualenv py3test in C:\Work\py3test\.venv
Updating dependenciesResolving dependencies... (0.4s)
(pytest and dependent packages will be installed)
> poetry run python --version
Python 3.7.5
Next, create a project for Python 2.7. The points are the following two points.
--Change the dependent Python version to 2.7 --pytest supports Python 2.7 up to 4.6 series
> cd ..
> poetry new py2test
Created package py2test in py2test
> cd py2test
[Important] Here pyproject.Edit toml.
python = "^3.7" → python = "^2.7"
pytest = "^5.2" → pytest = "^4.6"
> poetry env use C:\Python27\python.exe
Creating virtualenv py2test in C:\Work\py2test\.venv
Using virtualenv: C:\Work\py2test\.venv
> poetry install
Updating dependencies
Resolving dependencies... (1.1s)
(pytest and dependent packages will be installed)
> poetry run python --version
Python 2.7.17
For confirmation, let's move to the project for Python 3.7.
> cd ..\py3test
> poetry run python --version
Python 3.7.5
You can see that the Python virtual environment is used properly depending on the current folder. This is the end of the experiment.
I was able to operate the virtual environment with poetry using Py Launcher instead of pyenv on Windows. I think you can probably use it this way, but for now I think Pipenv is still easier on Windows.
Recommended Posts