<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>python on Ceda EI&#39;s Blog</title>
    <link>https://cedaei.com/tags/python/</link>
    <description>Recent content in python on Ceda EI&#39;s Blog</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <copyright>CC-BY-SA 4.0</copyright>
    <lastBuildDate>Sun, 19 Sep 2021 00:00:00 +0000</lastBuildDate>
    
	<atom:link href="https://cedaei.com/tags/python/index.xml" rel="self" type="application/rss+xml" />
    
    
    <item>
      <title>Improving Python Dependency Management With pipx and Poetry</title>
      <link>https://cedaei.com/posts/python-poetry-pipx/</link>
      <pubDate>Sun, 19 Sep 2021 00:00:00 +0000</pubDate>
      
      <guid>https://cedaei.com/posts/python-poetry-pipx/</guid>
      <description>&lt;p&gt;Over time, how I develop applications in python has changed noticeably. I will
divide the topic into three sections and see how they tie into each other at
the end.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Development&lt;/li&gt;
&lt;li&gt;Packaging&lt;/li&gt;
&lt;li&gt;Usage&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;development&#34;&gt;Development&lt;/h2&gt;
&lt;p&gt;Under development, the issues I will focus on are the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Dependency Management&lt;/li&gt;
&lt;li&gt;Virtualenvs and managing them&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Historically, the way to do dependency management was through
&lt;code&gt;requirements.txt&lt;/code&gt;.  I found &lt;code&gt;requirements.txt&lt;/code&gt; hard to manage. In that setup,
adding a dependency and installing it was two steps:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Add the package &lt;code&gt;bar&lt;/code&gt; to &lt;code&gt;requirements.txt&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Either do &lt;code&gt;pip install bar&lt;/code&gt; or &lt;code&gt;pip install -r requirements.txt&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;While focused on development, I would often forget one or both of these steps.
Also, the lack of a lock file was a small downside for me (could be a much
larger downside for others).  The separation between &lt;code&gt;pip&lt;/code&gt; and
&lt;code&gt;requirements.txt&lt;/code&gt; can also easily lead you to accidentally depend on packages
installed on your system or in your virtualenv but not specified in your
&lt;code&gt;requirements.txt&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Managing virtualenvs was also difficult. As a virtualenv and a project are not
related, you need a directory structure. Otherwise, you can&amp;rsquo;t tell which
virtualenv is being used for which project. You can use the same virtualenvs
for multiple projects, but that partially defeats the point of virtualenvs and
makes &lt;code&gt;requirements.txt&lt;/code&gt; more error-prone (higher chances of forgetting to add
packages to it). The approach generally used is one of the following two:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;foo/
├── foo_src/
└── foo_venv/
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;or&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;foo_src/
└── venv/
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I preferred the second one as the first one nests the source code one
directory deeper.&lt;/p&gt;
&lt;h3 id=&#34;a-new-standard---pyprojecttoml&#34;&gt;A new standard - &lt;code&gt;pyproject.toml&lt;/code&gt;&lt;/h3&gt;
&lt;p&gt;In &lt;a href=&#34;https://www.python.org/dev/peps/pep-0518/&#34; 
  
   target=&#34;_blank&#34; rel=&#34;noreferrer noopener&#34; 
&gt;PEP-518&lt;/a&gt;
, python standardized
the &lt;code&gt;pyproject.toml&lt;/code&gt; file which allows users to choose alternate build systems
for package generation.&lt;/p&gt;
&lt;p&gt;One such project that provides an alternate build system is
&lt;a href=&#34;https://python-poetry.org/&#34; 
  
   target=&#34;_blank&#34; rel=&#34;noreferrer noopener&#34; 
&gt;Poetry&lt;/a&gt;
. Poetry hits the nail on the head and
solves my major gripes with traditional tooling.&lt;/p&gt;
&lt;h3 id=&#34;poetry-and-virtualenvs&#34;&gt;Poetry and virtualenvs&lt;/h3&gt;
&lt;p&gt;Poetry manages the virtualenvs automatically and keeps track of which project
uses which virtualenv automatically. Working on an existing project which uses
poetry is as simple as this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;$ git clone https://gitlab.com/ceda_ei/verlauf
$ poetry install
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;poetry install&lt;/code&gt; command sets up the virtualenv, install all the required
dependencies inside that, and sets up any commands accordingly (I will get to
this soon).  To activate the virtualenv, simply run:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;. &amp;quot;$(poetry env info --path)/bin/activate&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I wrap this in a small function which lets me toggle it quickly:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;function poet() {
	POET_MANUAL=1
	if [[ -v VIRTUAL_ENV ]]; then
		deactivate
	else
		. &amp;quot;$(poetry env info --path)/bin/activate&amp;quot;
	fi
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Running &lt;code&gt;poet&lt;/code&gt; activates the virtualenv if it is not active and deactivates it if
it is active. To make things even easier, I automatically activate and
deactivate the virtualenv as I enter and leave the project directory.  To do
so, simply drop this in your &lt;code&gt;.bashrc&lt;/code&gt;.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;function find_in_parent() {
	local path
	IFS=&amp;quot;/&amp;quot; read -ra path &amp;lt;&amp;lt;&amp;lt;&amp;quot;$PWD&amp;quot;
	for ((i=${#path[@]}; i &amp;gt; 0; i--)); do
		local current_path=&amp;quot;&amp;quot;
		for ((j=1; j&amp;lt;i; j++)); do
			current_path=&amp;quot;$current_path/${path[j]}&amp;quot;
		done
		if [[ -e &amp;quot;${current_path}/$1&amp;quot; ]]; then
			echo &amp;quot;${current_path}/&amp;quot;
			return
		fi
	done
	return 1
}

function auto_poet() {
	ret=&amp;quot;$?&amp;quot;
	if [[ -v POET_MANUAL ]]; then
		return $ret
	fi
	if find_in_parent pyproject.toml &amp;amp;&amp;gt; /dev/null; then
		if [[ ! -v VIRTUAL_ENV ]]; then
		    if BASE=&amp;quot;$(poetry env info --path)&amp;quot;; then
			. &amp;quot;$BASE/bin/activate&amp;quot;
			PS1=&amp;quot;&amp;quot;
		    else
			POET_MANUAL=1
		    fi
		fi
	elif [[ -v VIRTUAL_ENV ]]; then
		deactivate
	fi
	return $ret
}

PROMPT_COMMAND=&amp;quot;auto_poet;$PROMPT_COMMAND&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This ties in well with the &lt;code&gt;poet&lt;/code&gt; function; if you use &lt;code&gt;poet&lt;/code&gt; anytime in a bash
session, activation switches from automatic to manual and changing directories
no longer auto-toggles the virtualenv.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://cedaei.com/images/auto_poet.webp&#34; alt=&#34;auto_poet and poet in action&#34;&gt;&lt;/p&gt;
&lt;h3 id=&#34;poetry-and-dependency-management&#34;&gt;Poetry and dependency management&lt;/h3&gt;
&lt;p&gt;Instead of using &lt;code&gt;requirements.txt&lt;/code&gt;, poetry stores the dependencies inside
&lt;code&gt;pyproject.toml&lt;/code&gt;.  Poetry is more strict compared to &lt;code&gt;pip&lt;/code&gt; in resolving
versioning issues.  Dependencies and dev-dependencies are stored inside
&lt;code&gt;tool.poetry.dependencies&lt;/code&gt; and &lt;code&gt;tool.poetry.dev-dependencies&lt;/code&gt; respectively.
Here is an example of a &lt;code&gt;pyproject.toml&lt;/code&gt; for a project I am working on.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-toml&#34;&gt;[tool.poetry]
name = &amp;quot;bells&amp;quot;
version = &amp;quot;0.3.0&amp;quot;
description = &amp;quot;Bells is a program for keeping track of sound recordings.&amp;quot;
authors = [&amp;quot;Ceda EI &amp;lt;ceda_ei@webionite.com&amp;gt;&amp;quot;]
license = &amp;quot;GPL-3.0&amp;quot;
readme = &amp;quot;README.md&amp;quot;
homepage = &amp;quot;https://gitlab.com/ceda_ei/bells.git&amp;quot;
repository = &amp;quot;https://gitlab.com/ceda_ei/bells.git&amp;quot;

[tool.poetry.dependencies]
python = &amp;quot;&amp;gt;=3.7,&amp;lt;3.11&amp;quot;
click = &amp;quot;^8.0.1&amp;quot;
questionary = &amp;quot;^1.10.0&amp;quot;
sounddevice = &amp;quot;^0.4.2&amp;quot;
SoundFile = &amp;quot;^0.10.3&amp;quot;
numpy = &amp;quot;^1.21.2&amp;quot;

[tool.poetry.dev-dependencies]

[build-system]
requires = [&amp;quot;poetry-core&amp;gt;=1.0.0&amp;quot;]
build-backend = &amp;quot;poetry.core.masonry.api&amp;quot;

# I will talk about this section soon
[tool.poetry.scripts]
bells = &amp;quot;bells.__main__:main&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;One of the upsides of poetry is that you don&amp;rsquo;t have to manage the dependencies
in &lt;code&gt;pyproject.toml&lt;/code&gt; file yourself. Poetry adds an &lt;code&gt;npm&lt;/code&gt;-like interface for
adding and removing dependencies.  To add a dependency to your project, simply
run &lt;code&gt;poetry add bar&lt;/code&gt; and it will add it to your &lt;code&gt;pyproject.toml&lt;/code&gt; file and
install it in the virtualenv as well. To remove a dependency, just run &lt;code&gt;poetry remove bar&lt;/code&gt;. For development dependencies, just add the &lt;code&gt;--dev&lt;/code&gt; flag to the
commands.&lt;/p&gt;
&lt;h2 id=&#34;packaging&#34;&gt;Packaging&lt;/h2&gt;
&lt;p&gt;Since poetry replaces the build system, we can now configure the build using
poetry via &lt;code&gt;pyproject.toml&lt;/code&gt;. Inside &lt;code&gt;pyproject.toml&lt;/code&gt;, the &lt;code&gt;tool.poetry&lt;/code&gt; section
stores all the build info needed; &lt;code&gt;tool.poetry&lt;/code&gt; contains the metadata,
&lt;code&gt;tool.poetry.dependencies&lt;/code&gt; contains the dependencies, &lt;code&gt;tool.poetry.source&lt;/code&gt;
contains private repository details (in case, you don&amp;rsquo;t want to use PyPi).&lt;/p&gt;
&lt;p&gt;One of the options is &lt;code&gt;tool.poetry.scripts&lt;/code&gt;. It contains scripts that the
project exposes. This replaces &lt;code&gt;console_scripts&lt;/code&gt; in &lt;code&gt;entry_points&lt;/code&gt; of
&lt;code&gt;setuptools&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;For example,&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-toml&#34;&gt;[tool.poetry.scripts]
foobar = &amp;quot;foo.bar:main&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This will add a script named &lt;code&gt;foobar&lt;/code&gt; in your &lt;code&gt;PATH&lt;/code&gt;. Running that is
equivalent to running the following script&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-python&#34;&gt;from foo.bar import main

if __name__ == &amp;quot;__main__&amp;quot;:
    main()
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For further details, check the
&lt;a href=&#34;https://python-poetry.org/docs/pyproject/&#34; 
  
   target=&#34;_blank&#34; rel=&#34;noreferrer noopener&#34; 
&gt;reference&lt;/a&gt;
.&lt;/p&gt;
&lt;p&gt;Poetry also removes the need for manually doing editable installs (&lt;code&gt;pip install -e .&lt;/code&gt;).  The package is automatically installed as editable when you run
&lt;code&gt;poetry install&lt;/code&gt;. Any scripts specified in &lt;code&gt;tool.poetry.scripts&lt;/code&gt; are
automatically available in your &lt;code&gt;PATH&lt;/code&gt; when you activate the &lt;code&gt;venv&lt;/code&gt;.&lt;sup id=&#34;fnref:1&#34;&gt;&lt;a href=&#34;#fn:1&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;To build the package, simply run &lt;code&gt;poetry build&lt;/code&gt;. This will generate a wheel and
a tarball in the dist folder.&lt;/p&gt;
&lt;p&gt;To publish the package to PyPi (or another repo), simply run &lt;code&gt;poetry publish&lt;/code&gt;.
You can combine the build and publish into one command with &lt;code&gt;poetry publish --build&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src=&#34;https://cedaei.com/images/poetry_build.webp&#34; alt=&#34;example of poetry build&#34;&gt;&lt;/p&gt;
&lt;h2 id=&#34;usage&#34;&gt;Usage&lt;/h2&gt;
&lt;p&gt;This part is more user-facing rather than dev-facing. If you want to use two
packages globally that expose some scripts to the user, (e.g. &lt;code&gt;awscli&lt;/code&gt;,
&lt;code&gt;youtube-dl&lt;/code&gt;, etc.) the general approach to do so is to run something like &lt;code&gt;pip install --user youtube-dl&lt;/code&gt;. This install the package at the user level and
exposes the script through &lt;code&gt;~/.local/bin/youtube-dl&lt;/code&gt;. However, this installs
all the packages at the same user level. Hypothetically, if you have two
packages &lt;code&gt;foo&lt;/code&gt; and &lt;code&gt;bar&lt;/code&gt; which have conflicting dependencies, this causes an
issue. If you run,&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;$ pip install foo
$ pip install bar
$ bar # works
$ foo # breaks because of dependency mismatch
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;While installing &lt;code&gt;bar&lt;/code&gt;, &lt;code&gt;pip&lt;/code&gt; will install the dependencies for &lt;code&gt;bar&lt;/code&gt; which
will break &lt;code&gt;foo&lt;/code&gt; after warning you&lt;sup id=&#34;fnref:2&#34;&gt;&lt;a href=&#34;#fn:2&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;2&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;To solve this, there is &lt;a href=&#34;https://github.com/pypa/pipx&#34; 
  
   target=&#34;_blank&#34; rel=&#34;noreferrer noopener&#34; 
&gt;&lt;code&gt;pipx&lt;/code&gt;&lt;/a&gt;
. Pipx installs
each package in a separate virtualenv without requiring the user to activate
said virtualenv before using the package.&lt;sup id=&#34;fnref:3&#34;&gt;&lt;a href=&#34;#fn:3&#34; class=&#34;footnote-ref&#34; role=&#34;doc-noteref&#34;&gt;3&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;
&lt;p&gt;In the same scenario as before, doing the following works just fine.&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;$ pipx install foo
$ pipx install bar
$ bar # works
$ foo # also works
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this scenario, both &lt;code&gt;bar&lt;/code&gt; and &lt;code&gt;foo&lt;/code&gt; are installed in separate virtualenvs so
the dependency conflict doesn&amp;rsquo;t matter.&lt;/p&gt;
&lt;h2 id=&#34;some-more-things-from-my-bashrc&#34;&gt;Some more things from my bashrc&lt;/h2&gt;
&lt;pre&gt;&lt;code class=&#34;language-bash&#34;&gt;
function wrapper_no_poet() {
	local last_env
	if [[ -v VIRTUAL_ENV ]]; then
		last_env=&amp;quot;$VIRTUAL_ENV&amp;quot;
		deactivate
	fi
	&amp;quot;$@&amp;quot;
	ret=$?
	if [[ -v last_env ]]; then
		. &amp;quot;$last_env/bin/activate&amp;quot;
	fi
	return $ret
}

alias wnp=&#39;wrapper_no_poet&#39;
alias pm=&#39;POET_MANUAL=1&#39;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Prefixing any command with &lt;code&gt;wnp&lt;/code&gt; runs it outside the virtualenv if a virtualenv
is active. Running &lt;code&gt;pm&lt;/code&gt; turns off automatic virtualenv activation.&lt;/p&gt;
&lt;section class=&#34;footnotes&#34; role=&#34;doc-endnotes&#34;&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li id=&#34;fn:1&#34; role=&#34;doc-endnote&#34;&gt;
&lt;p&gt;This also allows for a nice switch between the development and production
versions of the app. Essentially, when the virtualenv is active, you are
using the development script while when it is deactivated, you are using
the global (likely production) version.&amp;#160;&lt;a href=&#34;#fnref:1&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:2&#34; role=&#34;doc-endnote&#34;&gt;
&lt;p&gt;To be precise, it will warn you that it broke &lt;code&gt;foo&lt;/code&gt; but will still
continue with the installation&amp;#160;&lt;a href=&#34;#fnref:2&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li id=&#34;fn:3&#34; role=&#34;doc-endnote&#34;&gt;
&lt;p&gt;For development, poetry also provides &lt;code&gt;poetry run&lt;/code&gt; which runs a file
without having to activate the virtualenv.&amp;#160;&lt;a href=&#34;#fnref:3&#34; class=&#34;footnote-backref&#34; role=&#34;doc-backlink&#34;&gt;&amp;#x21a9;&amp;#xfe0e;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/section&gt;
</description>
    </item>
    
  </channel>
</rss>
