In part 1, we have talked about how to install and use Pyenv, and now, it’s time to talk about Poetry.
I think there are a lot of articles out there to tell you why you should use Poetry instead of pip or other pakcage managers. In simple words, Poetry is equal virtualenv
+ pip
.
I’m not profession in technical writting, so not going to write everything down again, but here are the articles that I think you can read as the reference. English, Mandarin.
The key thing I like about is it helps you to manage packages’ dependencies.
Anyways, let’s take a look!
To install Poetry, it’s quite simple, just refer their official docs (https://python-poetry.org/docs/)
For Linux or MacOS, just enter this in cmd
curl -sSL https://install.python-poetry.org | python3 -
For Windows, just open PowerShell, then enter
(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py -
If it doesn’t work, then try
(Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | python -
After the installation finished, you can enter to see if it works.
poetry --version
If it doesn’t, then on the official docs, it says, you need to add poetry wrapper to your environment path
For Linux or MacOS, it’s at
$HOME/.local/bin
You can simply just type…
$HOME/.local/bin/poetry
or you can add it to path and export it in your .zshrc
或.bashrc
to get rid of the full path.
For Windows, it’s at
%APPDATA%\Python\Script
I didn’t encounter any issue to add Poetry to environment Path in my Ubuntu or MacOS, but I got error messages on Windows like
The Term Is Not Recognized As The Name Of A Cmdlet
To solve this, you need to find where your poetry at…
For me as an example, it locates at
C:\Users\{username}\AppData\Roaming\pypoetry\venv\Scripts
You need to add this to your environment path and it should solve the problem.
Once Poetry works, then we can move to the next step.
UPDATE 2024.FEB.17th
I just bought a Macbook Pro M3 Max, and tried to set up env on my new macbook. However, I encounter a new issue.
Installing Poetry (1.7.1): Creating environment
Traceback (most recent call last):
File "<stdin>", line 945, in <module>
File "<stdin>", line 923, in main
File "<stdin>", line 560, in run
File "<stdin>", line 581, in install
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/contextlib.py", line 117, in __enter__
return next(self.gen)
File "<stdin>", line 653, in make_env
File "<stdin>", line 639, in make_env
File "<stdin>", line 317, in make
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/venv/__init__.py", line 66, in __init__
self.symlinks = should_use_symlinks(symlinks)
File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/venv/__init__.py", line 31, in should_use_symlinks
raise Exception("This build of python cannot create venvs without using symlinks")
Exception: This build of python cannot create venvs without using symlinks
This issue was also raised on Github (issue 24).
There were couple solutions that you can find on stackoverflow post.
Here is the one I chose to get things done.
curl -sSL https://install.python-poetry.org | sed 's/symlinks=False/symlinks=True/' | python3 -
However, after poetry installed, I encountered another issue.
pypoetry/venv/lib/python3.9/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020
I had tried multiple solutions that someone posted on stackoverflow, but none of it works perfectly. Eventually, I decide to uninstall poetry and reinstall it via pyenv python.
pyenv install 3.12.0
pyenv global 3.12.0
echo 'eval "$(pyenv init --path)"' >> ~/.zprofile
source ~/.zprofile
curl -sSL https://install.python-poetry.org | python3 -
# Check if the installation works
poetry --version
搶先發佈留言