<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sudarsaan</title>
    <description>The latest articles on DEV Community by Sudarsaan (@artemi8).</description>
    <link>https://dev.to/artemi8</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1314158%2F10da3054-93ab-4c7e-924e-9919697a78b9.jpeg</url>
      <title>DEV Community: Sudarsaan</title>
      <link>https://dev.to/artemi8</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/artemi8"/>
    <language>en</language>
    <item>
      <title>Managing python projects like a pro!</title>
      <dc:creator>Sudarsaan</dc:creator>
      <pubDate>Mon, 18 Mar 2024 18:20:44 +0000</pubDate>
      <link>https://dev.to/artemi8/managing-python-projects-like-a-pro-1li4</link>
      <guid>https://dev.to/artemi8/managing-python-projects-like-a-pro-1li4</guid>
      <description>&lt;p&gt;Hello folks! When I first started with python and was working on some hobby projects I used to struggle managing dependencies and sharing the whole code base to my friends to test it out. Another pain was to resolve python versions for some packages or projects I used. When I first heard about &lt;code&gt;poetry&lt;/code&gt; and &lt;code&gt;pyenv&lt;/code&gt; I was like, &lt;em&gt;"holy sheeet which island I was on"&lt;/em&gt;. Here we will see one among many professional ways to set up python using &lt;code&gt;pyenv&lt;/code&gt;and installing poetry for dependency management. This blog gonna be bit long, but trust me it will be save lots of time in the future.&lt;/p&gt;

&lt;h4&gt;
  
  
  Note:
&lt;/h4&gt;

&lt;p&gt;I am only going to cover installation and setup for windows as I am on one. I am basically writing this for myself which will help me get back to it whenever I need.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fel9jciqnsk8wtnp9v3po.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fel9jciqnsk8wtnp9v3po.gif" alt="By Giphy" width="480" height="240"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing &lt;code&gt;pyenv&lt;/code&gt; on Windows using &lt;a href="https://github.com/pyenv-win/pyenv-win"&gt;pyenv-win&lt;/a&gt;
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Installing using powershell&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Run the below command in the powershell, if you find this blog post to be too old, please follow the steps in the official repository of &lt;a href="https://github.com/pyenv-win/pyenv-win"&gt;pyenv-win&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Invoke-WebRequest -UseBasicParsing -Uri "https://raw.githubusercontent.com/pyenv-win/pyenv-win/master/pyenv-win/install-pyenv-win.ps1" -OutFile "./install-pyenv-win.ps1"; &amp;amp;"./install-pyenv-win.ps1"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the command fail due to issues regarding permissions, try adding &lt;code&gt;-Scope Currentuser&lt;/code&gt; _(Only if you are in an admin account and using powershell) or simply run the powershell as administrator.&lt;/p&gt;

&lt;p&gt;Once done with the installation, verify it running the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pyenv --version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should see something like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pyenv 3.x.x
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's quickly install python. But before installing it, let's make use of &lt;code&gt;pyenv&lt;/code&gt; for which it is installed for, Yes you got it right! choosing the version of the python to be installed easily and switching it back and forth to the versions we need.&lt;/p&gt;

&lt;p&gt;You can see the python versions available to install using &lt;code&gt;pyenv&lt;/code&gt; by running following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pyenv install -l
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then choose a version you need for your project and install it using.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pyenv install &amp;lt;version&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once installation is successful, make the installed python as the global version using the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pyenv global &amp;lt;version&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What this basically does is, it updates/sets the path of the python interpreter in the environment variable for us to use. This saves us so much time and makes our life easier by smoothly moving from one version from another easily.&lt;/p&gt;

&lt;p&gt;Hurrayy! We successfully installed python with the version we need. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff6ufnej1g8hmvv0o9lcj.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff6ufnej1g8hmvv0o9lcj.gif" alt="By Giphy" width="500" height="281"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hold on a sec though, let's verify all the wheels are moving appropriately.&lt;/p&gt;

&lt;p&gt;Let's test is using following commands&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pyenv version
python -c "import sys; print(sys.executable)"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First one's output should give you something like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;version&amp;gt; (set by \path\to\.pyenv\pyenv-win\.python-version)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Second one's output should be something like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;\path\to\.pyenv\pyenv-win\versions\&amp;lt;version&amp;gt;\python.exe
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If these are working, then yes we managed to setup python. Next we will go ahead and install &lt;code&gt;poetry&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExY3lwdDltcHV5bm1xYzR0b3kzYmcwZzZ4c2NkOGIwamdqYWN4c242ZyZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/ENu04tM82xjsjPzyNz/giphy-downsized.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExY3lwdDltcHV5bm1xYzR0b3kzYmcwZzZ4c2NkOGIwamdqYWN4c242ZyZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/ENu04tM82xjsjPzyNz/giphy-downsized.gif" alt="By Giphy" width="480" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Poetry Installation
&lt;/h2&gt;

&lt;p&gt;Before installing poetry let's understand what is scoop for windows, pipx and why is recommended. If you are in a hurry please skip to the installation part of the blog below.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scoop
&lt;/h3&gt;

&lt;p&gt;Scoop is a command-line installer for Windows, aimed at making it easier for users to manage software installations and maintain a clean system. It's designed with developers and power users in mind but can be beneficial for any Windows user looking for an efficient way to manage software. Basically it makes our life easier when it comes to software installation of any sort. Scoop support installation for large number of software.  Check it out here &lt;a href="https://scoop.sh/"&gt;Scoop&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  pipx
&lt;/h3&gt;

&lt;p&gt;pipx is nothing but a tool for installing and running Python applications in isolated environments. It ensures that each application and its dependencies are contained in their own virtual environment, preventing conflicts with other applications or the system-wide Python environment. pipx allows users to easily install, run, and manage Python CLI tools globally while maintaining the benefits of environment isolation. This approach simplifies the management of Python tools and enhances security and stability by isolating dependencies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Next question you might get is, why do we need pipx for poetry?
&lt;/h3&gt;

&lt;p&gt;Poetry recommends installation using pipx and here are the main reasons.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Environment Isolation: pipx installs Python applications in isolated virtual environments. This isolation ensures that Poetry and its dependencies do not interfere with the system-wide Python environment or other project-specific environments. It helps prevent dependency conflicts and ensures that the Python ecosystem on your system remains clean and manageable.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Global Accessibility with Isolation: While pipx installs Poetry in an isolated environment, it also makes Poetry globally accessible from the command line. This means you can run Poetry commands in any directory to manage project dependencies, without needing to activate a specific virtual environment. This setup combines the convenience of global access with the benefits of environment isolation.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Let's install Poetry now!
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Make sure you installed pipx on your system. if you have not done it follow this official &lt;a href="https://pipx.pypa.io/stable/"&gt;pipx installation documentation&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Then using pipx install poetry with the below command. If you wish to install a specific version of poetry for some reason follow this &lt;a href="https://python-poetry.org/docs/"&gt;poetry documentation&lt;/a&gt;.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pipx install poetry
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hurray! you are finally set. Now you can use poetry by navigating inside a directory where your project is and run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;poetry init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It will ask you series of questions like package name, version and dependencies it needs to initialize etc. Follow the instructions and finish the procedure and you will have a project.toml and poetry.lock files.  &lt;/p&gt;

&lt;p&gt;To install any package to your project all you have to do is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;poetry add &amp;lt;package-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here by default PyPi will be the default repository for installing the packages but it supports other sources too. You can read this &lt;a href="https://python-poetry.org/docs/repositories/"&gt;documentation&lt;/a&gt;to understand more about the procedure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Other important note on poetry installation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Adding development packages
&lt;/h3&gt;

&lt;p&gt;Development dependencies are packages that are needed for development purposes, such as testing, linting, or building documentation, but not for running the actual project code. Here's how you add a development package. Distinguishing between development and production dependencies keeps your project's environment clean and minimal. Only the packages necessary for running the project are installed in production, reducing the deployment size and potential attack surface for security vulnerabilities. You can install dev packages using the following command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;poetry add &amp;lt;package-name&amp;gt; --group dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Understanding pyproject.toml file
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;pyproject.toml&lt;/code&gt; file is divided into several sections, each serving a specific purpose:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[tool.poetry]&lt;/code&gt;: This section contains the project metadata like name, version, description, authors, and more. It's essential for identifying the project and its versioning.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[tool.poetry.dependencies]&lt;/code&gt;: Lists the project's dependencies. These are packages your project needs to run. Poetry automatically manages these dependencies and their versions to avoid conflicts.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[tool.poetry.dev-dependencies]&lt;/code&gt;: Specifies the development dependencies. These are packages required for development tasks, like testing, linting, or documentation, but not needed for running the project.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[build-system]&lt;/code&gt;: Defines the build system requirements, specifying which tool (like Poetry) should be used to build the project and its dependencies.&lt;/p&gt;

&lt;p&gt;In summary, the pyproject.toml file is a powerful tool for Python project management, offering a standardized way to specify project settings and dependencies. By making full use of pyproject.toml, you can enhance project portability, simplify configuration management, and streamline development and deployment processes.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is the &lt;code&gt;poetry.lock&lt;/code&gt; File?
&lt;/h3&gt;

&lt;p&gt;When you specify dependencies in the pyproject.toml file using Poetry, you often do so without specifying exact versions (or using very broad constraints). This approach keeps your project flexible but can lead to inconsistencies across installations if dependencies are updated.&lt;/p&gt;

&lt;p&gt;The poetry.lock file is automatically generated or updated by Poetry when you add, update, or install dependencies. It contains a detailed list of your project's direct and indirect dependencies, including the exact version and checksum of each package. This specificity ensures that no matter where or when you install your project's dependencies, Poetry will install the exact same versions, as specified in the poetry.lock file.&lt;/p&gt;

&lt;p&gt;That's it, now you have a good idea of how to setup a project environment professionally. you are good to go!&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;we dive into the essential tools and practices for effective Python project management. Highlighting the initial challenges of dependency management and Python version conflicts, this post introduces &lt;code&gt;pyenv&lt;/code&gt;, &lt;code&gt;poetry&lt;/code&gt;, and &lt;code&gt;pipx&lt;/code&gt;as game-changing tools for any Python developer's toolkit.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Starting with pyenv for Python version management, we explore how to install and utilize this tool on Windows to seamlessly switch between Python versions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Next, we delve into poetry for dependency management, explaining its advantages, such as environment isolation and simplification of dependency handling. We also discuss the importance of using pipx to install Python CLI tools globally while keeping them isolated. Through step-by-step instructions, the post guides on setting up these tools to create a more efficient and streamlined development workflow. By embracing these tools, we can save time, avoid common pitfalls, and focus on building great projects with confidence.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Best wishes with your projects and let me know if there is anything in the comment section.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExMmxwMHZsaHBmejlhdml2a3Bnd3VoemtrczRrbHB2bTNxMjl4aXFybyZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/G0vYU697uKl0IiIJO2/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/v1.Y2lkPTc5MGI3NjExMmxwMHZsaHBmejlhdml2a3Bnd3VoemtrczRrbHB2bTNxMjl4aXFybyZlcD12MV9pbnRlcm5hbF9naWZfYnlfaWQmY3Q9Zw/G0vYU697uKl0IiIJO2/giphy.gif" alt="By Giphy" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;See ya!&lt;/p&gt;

</description>
      <category>poetry</category>
      <category>pipx</category>
      <category>pyenv</category>
      <category>python</category>
    </item>
  </channel>
</rss>
