life is too short for a diary

EnvCommandError in Windows using Poetry

Tags: windows python poetry bug

Poetry is a great dependency management tool in python. It's better than managing a flat file like `requirements.txt`. There are also other great tools like pipenv. However I found poetry much simpler in resolving dependencies.

Recently poetry has been crashing on my Windows machine. However, I have not encountered this issue in other platform like Linux. Folks who are unfamiliar with poetry, below is a quick refresher.

In cygwin, install poetry

$ curl -sSL | python

Check the curent version

$ poetry -version 

Initialize poetry by creating a pyproject.toml file.

$ poetry init

Now add some library like pandas

$ poetry add pandas

However this command is causing the poetry to crash

Creating virtualenv myproject-xkRwmYf0-py3.7 in .venv

[Env CommandError]
Command ['.venv\\myproject\\Scripts\\python.exe', '-'] errored with the follwiong return code 1, and output:
The system cannot find the path specified
input was : import sys

is hasattr(sys, "real_prefix"):
elif hasattr(sys, "base_prefix"):

Since poetry tries to create virtualenv under .venv folder, there's a quick workaround by removing carriage return from activate Scripts

$ perl -pe 's/\r$//' < .venv/myproject-xkRwmYf0-py3.7/Scripts/activate > activate
$ mv activate .venv/myproject-xkRwmYf0-py3.7/Scripts/activate 

Then you can manually activate the virtualenv like this

$ . .venv/myproject-xkRwmYf0-py3.7/Scripts/activate 
(myproject-xkRwmYf0-py3.7) $ 

Then you can simply install dependencies like

(myproject-xkRwmYf0-py3.7) $  poetry add pandas

Lastly , you can deactive the virtualenv like

(myproject-xkRwmYf0-py3.7) $ deactivate

This manual method may not be the best solution. May be future updates of poetry fixes this issue on Windows.

comments powered by Disqus