- I am on the latest Poetry version.
- I have searched the issues of this repo and believe that this is not a duplicate.
- If an exception occurs when executing a command, I executed it again in debug mode (-vvv option).
- OS version and name: Arch Linux x86_64
- Poetry version: 1.1.2
Issue
After activating virtual env using poetry shell, running deactivate command deactivates the env, but poetry thinks it's still active.
Steps to reproduce:
Before,
$ poetry --version Poetry version 1.1.2 $ which python /usr/bin/python
Activate the env,
$ poetry shell -vvv Using virtualenv: /home/eb/.cache/pypoetry/virtualenvs/alpha-28-NyTHq-py3.8 Spawning shell within /home/eb/.cache/pypoetry/virtualenvs/alpha-28-NyTHq-py3.8 . /home/eb/.cache/pypoetry/virtualenvs/alpha-28-NyTHq-py3.8/bin/activate $ . /home/eb/.cache/pypoetry/virtualenvs/alpha-28-NyTHq-py3.8/bin/activate $ which python /home/eb/.cache/pypoetry/virtualenvs/alpha-28-NyTHq-py3.8/bin/python
Deactivate using deactivate, It says it's already active.
$ deactivate $ which python /usr/bin/python $ poetry shell -vvv Using virtualenv: /home/eb/.cache/pypoetry/virtualenvs/alpha-28-NyTHq-py3.8 Virtual environment already activated: /home/eb/.cache/pypoetry/virtualenvs/alpha-28-NyTHq-py3.8
It works properly if exit were used instead of deactivate.
The Python package management tool you needed yesterday
Photo by Jared Rice on UnsplashWhat is the first word that comes to mind with Python? Numpy, Pandas or SciKit-Learn? What about Poetry? No, not that kind of poetry.
Poetry : Python packaging and dependency management made easy
Like a beautiful poem recited over a moonlit night, Poetry is here to soothe the nerves you get at the thought of reproducing your Python environment. Well at least for me.
Introduction
Poetry is a Python dependency management tool that manages virtual environment configurations as well as all the packages in your project. Think of it as a tool that combines all the best features of Anaconda and venv. Poetry enables anyone the ability to produce deterministic builds of Python execution environment.
Poetry automatically creates and manages virtual environments in addition to adding or removing packages as you install/uninstall packages from your project.
Installation
We will be working from the terminal in the command line. This will mean you need to open a Terminal window on mac or a Powershell window on a Windows machine.
- There are different commands for mac and windows so choose the appropriate command from below.
Mac:
curl -sSL //raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -Windows:
(Invoke-WebRequest -Uri //raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py -UseBasicParsing).Content | python -2. Confirm that poetry has been installed correctly by typing:
poetry --versionIf you see a poetry version number, great!! This means you are ready to use poetry 🎉🎉🎉 .
Creating your first virtual environment
I am going to assume you have installed the version of Python you require. If you have not, pause here and install the version of Python you need from the main Python website.
- Once you have Python and Poetry installed, change the working directory to your main project directory.
2. Initialise Poetry with the command:
poetry init3. Work through the guide to create a pyproject.toml config file which will define the configurations of your Python environment. I have defined my Python version to be 3.9.7, change this value to the Python version you require. In my example below, I have chosen to not define the main dependencies interactively as I prefer to add them after.
4. Install the Python virtual environment.
poetry install5. Add Python packages to your project by replacing <PACKAGE_NAME> with the name of the package you wish to add. You can add multiple packages by separating each name with a space.
poetry add <PACKAGE_NAME>6. Activate your Python virtual environment and you should see the virtual environment’s unique identifier preceding your current folder location as shown below.
poetry shell7. To link your newly created virtual environment to Jupyter use the command below; replace poetry_python_env with the name, you wish to call it in Jupyter.
python -m ipykernel install --user --name=poetry_python_envExporting your Python build
Poetry makes it easy to share deterministic builds of Python environments. To do so, all you need to do is share or push the poetry.lock and pyproject.toml file to a repository.
As shown in the example below, the project working directory now contains a file called poetry.lock and pyproject.toml.
project├── poetry.lock
└── pyproject.toml
In a terminal window, navigate to the project directory and install the virtual environment:
poetry install
It is as easy as that.
If you have reached the end of this blog, I hope you have gained a new skill in Python package management. Please do leave a like and follow me on Medium and Twitter (@iLloydHamilton) for more data science-related content.
Watch this space.
More content at plainenglish.io. Sign up for our free weekly newsletter here.