<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Python Environments - Medium]]></title>
        <description><![CDATA[This publication aims to evaluate the best way to setup a Python environment depending on the goals of your specific project. This way, you can spend less time setting up your environment and more time creating value. - Medium]]></description>
        <link>https://medium.com/python-environments?source=rss----e25aae99fe98---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Python Environments - Medium</title>
            <link>https://medium.com/python-environments?source=rss----e25aae99fe98---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 12 Nov 2026 19:34:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/python-environments" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[uv Package Manager: Getting Started]]></title>
            <description><![CDATA[<div class="medium-feed-item"><p class="medium-feed-image"><a href="https://medium.com/python-environments/uv-package-manager-getting-started-a18ffec7694b?source=rss----e25aae99fe98---4"><img src="https://cdn-images-1.medium.com/max/1400/0*Oe17jOhTSo6P9cp6.png" width="1400"></a></p><p class="medium-feed-snippet">Python Environments</p><p class="medium-feed-link"><a href="https://medium.com/python-environments/uv-package-manager-getting-started-a18ffec7694b?source=rss----e25aae99fe98---4">Continue reading on Python Environments »</a></p></div>]]></description>
            <link>https://medium.com/python-environments/uv-package-manager-getting-started-a18ffec7694b?source=rss----e25aae99fe98---4</link>
            <guid isPermaLink="false">https://medium.com/p/a18ffec7694b</guid>
            <category><![CDATA[venv]]></category>
            <category><![CDATA[uv]]></category>
            <category><![CDATA[python-environment]]></category>
            <category><![CDATA[uv-package-manager]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Eric Flynn]]></dc:creator>
            <pubDate>Mon, 13 Oct 2025 16:05:37 GMT</pubDate>
            <atom:updated>2025-10-13T16:05:33.018Z</atom:updated>
        </item>
        <item>
            <title><![CDATA[Getting Started With Docker]]></title>
            <link>https://medium.com/python-environments/getting-started-with-docker-8ee56b6baa7a?source=rss----e25aae99fe98---4</link>
            <guid isPermaLink="false">https://medium.com/p/8ee56b6baa7a</guid>
            <category><![CDATA[pip]]></category>
            <category><![CDATA[docker-container]]></category>
            <category><![CDATA[virtual-machine]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Eric Flynn]]></dc:creator>
            <pubDate>Fri, 22 Jul 2022 14:30:06 GMT</pubDate>
            <atom:updated>2022-07-22T14:29:52.155Z</atom:updated>
            <content:encoded><![CDATA[<h4><a href="https://medium.com/python-environments">Python Environments</a></h4><h4>Docker, Virtual Machines, and Containerized Application Development With Python &amp; pip</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*JeCgltGjSHt2ZDMW" /><figcaption>Photo by Guillaume Bolduc on Unsplash</figcaption></figure><h3>Introduction</h3><p>If you’ve spent some time scrolling through medium stories in the data science community then you have undoubtedly come across a Docker article looking very similar to this one. Chances are that the article even had a similar picture of shipping containers like the one shown above. I was guilty of reading more than a few of these articles and still not fully grasping what a container is and how Docker works to build/deploy them. I understand that containers are a must for modern application development and that they are “lightweight and portable”, but I couldn’t fully grasp why.</p><p>The purpose of this article is to provide a clear and concise explanation of Docker containers in order to help you understand the <strong>why. </strong>I aim to explain from the bottom up how Docker differs from traditional virtual-machine solutions and why it is so revolutionary. Then I will walk through a basic example of how to create your first Docker container.</p><h4>What is a Virtual Machine?</h4><p>Before we dive into Docker and containers, let’s quickly touch on their distant older cousin, virtual machines. The concept of a virtual machine was introduced alongside the first personal computers in the early 1970’s. Computer resources were more expensive to provision back then, so researchers at IBM aimed to discover an economical method to distribute these physical resources among a large group of distinct users. Fast forward to today and virtualization has become the backbone of cloud computing. Companies like AWS and Google Cloud maintain massive data centers, and then provision virtual machines to users on-demand for extremely low cost.</p><p>A virtual machine operates nearly identically to a personal computer from the user’s point of view. <strong>Virtual machines will have their own “isolated” interface, storage, ram, and operating system.</strong> The key difference occurs in how the machine utilizes its physical hardware. On a standard non-virtual machine (think personal computer) physical hardware is directly tied to only that machine. With virtualization, we have multiple virtual machines that all pull from the same physical resources. The distribution of these resources is managed by a <strong>hypervisor</strong>.</p><h4>Container vs. Virtual Machine</h4><p>Now that we understand how virtual machines work, we can introduce what a container is and how the two differ.</p><blockquote>A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. -<a href="https://www.docker.com/resources/what-container/#:~:text=A%20container%20is%20a%20standard,one%20computing%20environment%20to%20another.">Docker</a></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vXPEWTqDShIfoORP.jpg" /><figcaption>Container vs. VM (<a href="https://www.bmc.com/blogs/containers-vs-virtual-machines/">https://www.bmc.com/blogs/containers-vs-virtual-machines/</a>)</figcaption></figure><p>The picture above highlights the key structural difference between containers and virtual machines. With the traditional virtual machine, a hypervisor manages multiple operating systems running in parallel that all share the same compute resources (The infrastructure). The hypervisor can allocate memory, processors and storage to each VM as needed, but each virtual machine needs its own dedicated guest operating system. <strong>A container on the other hand, leverages the operating system of the host to run its applications.</strong> The container engine (Docker) can perform virtualization at the OS-level by acting as a middle man between the application and the host operating system.</p><p>This major structural difference in OS utilization is how containers can remain so lightweight and portable. They don’t have to carry around the baggage of a beefy operating system. Because of this, containers can be only a couple megabytes in size while virtual machines may take up gigabytes of storage. This allows for containers to be quickly commissioned in seconds whereas virtual machines may take a couple minutes.</p><h4>Infrastructure Of Docker</h4><p>Docker utilizes a client-server architecture to build and run its containers. Below are the main components.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*5ki-yGcQG3be6am21m5RZw.png" /><figcaption>Docker Architecture (<a href="https://docs.docker.com/get-started/overview/">https://docs.docker.com/get-started/overview/</a>)</figcaption></figure><h4>1. Docker Host</h4><p>The docker host is the operating system on which docker is installed. This will be your local machine or the cloud instance where you are hosting your containers.</p><h4><strong>2. Docker Client</strong></h4><p>The Docker Client is the primary user interface for Docker. Commands are issued from the command line, then translated to a http request and sent to the Docker daemon.</p><h4>3. Docker daemon</h4><p>The Docker daemon is a persistent background process for managing containers under a given Docker Host. The daemon listens for http requests from the docker client and will communicate with the host os to manage docker containers, images, network, and storage.</p><h4>3. Docker Registry</h4><p>A public, open-source registry that you can push/pull container images from.</p><h4>4. Docker Objects</h4><p><strong>Image: </strong>An image is the basic building block of a container. It contains a set of simple, read-only instructions that are used to construct a container. Oftentimes, images will stack on top of each other. For example, if we want to utilize a Python application in our container then we must first build a Linux image, then instruct our image to install Python as well. Images are stored in the form of a <strong>Dockerfile</strong>.</p><p><strong>Container: </strong>A container is the executable form of an image and contains applications to be ran. Containers are logically isolated from each other and easily managed with the Docker Client.</p><h3>How To Use Docker</h3><blockquote>Time to Dive In</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*vK_fBW-YssCWZ0Qr" /><figcaption>Photo by Oliver Sjöström on Unsplash</figcaption></figure><p>In the below sections, I will first give a very brief explanation of how to install Docker and then list out some of the most common Docker Client commands. Next, we will develop a Docker image from scratch and run a basic “Hello, World!” Python application on it. Finally I will explain the easiest method to pip install packages into a container.</p><h4>Installation</h4><p>Docker desktop is available for installation on Windows, Mac, and Linux and contains everything you will need to begin developing and running Docker containers. Installation instructions can be found <a href="https://docs.docker.com/get-docker/">here</a>. Note: If you are operating on a work computer where you do not have admin privileges then you may run into issues with Docker installation. Docker desktop has an extremely simple user interface so I will skip the detailed walkthrough.</p><h4>Docker Commands</h4><p>The primary method to manage and deploy Docker containers is via the command line on the host machine. Each command will start with “docker” and be followed by one of various base commands. Listed below are some of the most common commands.</p><blockquote><strong>docker search:</strong> Allows you to search all publicly available images from the Docker registry</blockquote><blockquote><strong>docker pull: </strong>Once we have the name of our desired image, this command allows us pull it into our local host</blockquote><blockquote><strong>docker images: </strong>Lists out each local image</blockquote><blockquote><strong>docker build:</strong> Build an image from a local Dockerfile</blockquote><blockquote><strong>docker run: </strong>Can build and deploy a container based on a saved image</blockquote><blockquote><strong>docker ps:</strong> Lists all running containers (Can include stopped with — a)</blockquote><blockquote><strong>docker stop:</strong> Stops a currently running container</blockquote><blockquote><strong>docker start:</strong> Starts a stopped container</blockquote><blockquote><strong>docker restart:</strong> Restarts a stopped container</blockquote><blockquote><strong>docker exec: </strong>Allows you to access the command line of a running container</blockquote><blockquote><strong>docker rm:</strong> Delete a stopped container and any volumes associated</blockquote><blockquote><strong>docker rename:</strong> Rename an existing container</blockquote><blockquote><strong>docker rmi: </strong>Deletes an image</blockquote><h4>Creating A Docker Container From Scratch</h4><p><strong>Step 1: Prepare your directory</strong></p><p>For this example we will need to create a new directory with two files in it. One of those files is our <em>Dockerfile,</em> which is used to construct our image. The other fill will be <em>main.py</em> which holds our Python “application”.</p><pre>.</pre><pre>├── Dockerfile</pre><pre>└── main.py</pre><p><strong>Step 2: Prepare your application script in <em>main.py </em>as shown below:</strong></p><p>For this application we will simply just print “Hello, World!”.</p><pre>#!/usr/bin/env python3<br>print(&quot;Hello, World!&quot;)</pre><p><strong>Step 3: Modify the Dockerfile as shown below:</strong></p><p>First, we import a base image containing Python using “FROM” and specify the latest version. Next, we will import our Python script <em>main.py</em> into our container with “COPY”. The “/” denotes the path to our file which is currently in the root directory. Finally, we can use CMD to specify a command to be executed when the container is run. In this case, we want Python to run our <em>main.py</em> script.</p><pre>FROM python:latest</pre><pre>COPY main.py /</pre><pre>CMD [&quot;python&quot;, &quot;./main.py&quot;]</pre><p><strong>Step 3: Create image</strong></p><p>To create an image from our Dockerfile we will use the docker build command from within the directory we created above. <strong>Make sure that you have the Docker service running on your computer. </strong>If not, then the daemon will not be able to receive your requests. There is no easy way to start the daemon from the command line on Mac OS or Windows so I recommend just opening Docker Desktop and letting it run in the background.</p><pre>docker build -t my-first-python-image .</pre><p><em>Note: The “-t” specifies a tag for your image which is essentially its name. The “.” specifies that you want to create the image from your current directory.</em></p><p>Now, let’s verify that the image was created with docker images .</p><pre>% docker images</pre><pre>REPOSITORY              TAG       IMAGE ID       CREATED      SIZE</pre><pre>my-first-python-image   latest    72fb37883adf   2 days ago   920MB</pre><p>We can also check our Docker Desktop.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FigUW3w8IocCDXT46aNCVg.png" /></figure><p><strong>Step 4: Run container from image</strong></p><p>Now that we have our image created, we can now create and run a container based on it. To do this we will use docker run .</p><pre>% docker run --name python-hello my-first-python-image</pre><pre>Hello, World!</pre><p><em>Note: Here, we will use “ — name” to assign a name to our container upon launching it. The way I think about this command is:</em></p><p><strong><em>Run a container named “python-hello” from “my-first-python-image”</em></strong></p><p>We can verify this container was created with the command line by using docker ps -a . In this case -a is used to show all running/stopped containers. Since our script is not persistent it will stop right after execution.</p><pre>% docker ps -a</pre><pre>CONTAINER ID   IMAGE                   COMMAND              CREATED         STATUS                     PORTS     NAMES</pre><pre>28e578e72d3e   my-first-python-image   &quot;python ./main.py&quot;   4 minutes ago   Exited (0) 4 minutes ago             python-hello</pre><p>We can also verify with Docker Desktop.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZMe53ZHjSET2mZuONKtnkw.png" /></figure><p><strong>Step 5: Re-run your container</strong></p><p>The beauty of Docker lies in the ability to easily re-run your containers once they have been created. To do this we will use docker start .</p><pre>% docker start python-hello -a</pre><pre>Hello, World!</pre><p>Note: The “-a” here is used so that we attach the output to our current command line. Without it, our container will run in detached mode and we wouldn’t see the output.</p><h3>How To Add Packages To Your Container</h3><p>The script above was extremely simple, and provides no real value beyond testing out how Docker works. Oftentimes, we will need to import third party packages like Pandas or Tensor Flow for any meaningful Python application. In the following section I will explain the easiest method to pip install a package into a Docker container.</p><h4>Installing Packages Via Dockerfile</h4><p>There are a handful of methods to pip install packages into your Docker container, but I feel that adding the pip installation into your Dockerfile is the easiest. For this example, we will modify the directory from above. Don’t worry, since you already have an image and container created you can still re-run them with ease!</p><p><strong>Step 1: </strong>Modify your Dockerfile</p><p>In order to install a package into our Dockerfile we must utilize the RUN command. Keep in mind that the base image that we pulled from is a Python image, which will come with pip already installed. If not, we would first have to install pip before utilizing it. Below we will add instructions in our Dockerfile to pip install pandas:</p><pre>FROM python:latest</pre><pre>RUN pip install pandas</pre><pre>COPY main.py /</pre><pre>CMD [&quot;python&quot;, &quot;./main.py&quot;]</pre><p><strong>Step 2</strong>: Modify your Python script</p><p>The next step involves importing pandas into our Python script to verify it was installed correctly. To do this, modify your <em>main.py</em> as below:</p><pre>#!/usr/bin/env python3<br>import pandas as pd<br>df = pd.DataFrame([1,2,3,4])<br>print(df)</pre><p><strong>Step 3</strong>: Build your image, and run a new container</p><p>This step will be executed the exact same way as above so I will skip the in -depth explanation.</p><pre>% docker build -t my-second-python-image .</pre><pre>...</pre><pre>% docker run --name python-pandas my-second-python-image</pre><pre>0</pre><pre>0  1</pre><pre>1  2</pre><pre>2  3</pre><pre>3  4</pre><p>We can see that the simple DataFrame was created and printed with no errors.</p><h3>Conclusion</h3><p>All in all, Docker is an extremely useful tool for any individual looking to advance their programming skills. With the rise of cloud computing, it is becoming ever more important to be able to produce agile scripts that can run with ease in a variety of computing environments. By virtualizing the OS, Docker was able to improve upon standard virtual machine technology and provide users with a truly portable and lightweight solution for application dev ops. I hope that this article was able to give you a fundamental understanding of Docker. Thank you for reading!</p><h3>References</h3><p><a href="https://www.idkrtm.com/history-of-virtualization/">https://www.idkrtm.com/history-of-virtualization/</a></p><p><a href="https://stackoverflow.com/questions/50333650/install-python-package-in-docker-file">https://stackoverflow.com/questions/50333650/install-python-package-in-docker-file</a></p><p><a href="https://www.freecodecamp.org/news/a-beginners-guide-to-docker-how-to-create-your-first-docker-application-cc03de9b639f/">https://www.freecodecamp.org/news/a-beginners-guide-to-docker-how-to-create-your-first-docker-application-cc03de9b639f/</a></p><p><a href="https://www.geeksforgeeks.org/how-to-run-a-python-script-using-docker/">https://www.geeksforgeeks.org/how-to-run-a-python-script-using-docker/</a></p><p><a href="https://www.docker.com/">https://www.docker.com/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8ee56b6baa7a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/python-environments/getting-started-with-docker-8ee56b6baa7a">Getting Started With Docker</a> was originally published in <a href="https://medium.com/python-environments">Python Environments</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learn Python venv In Under 5 Minutes]]></title>
            <link>https://medium.com/python-environments/learn-python-venv-in-under-5-minutes-42205842cabd?source=rss----e25aae99fe98---4</link>
            <guid isPermaLink="false">https://medium.com/p/42205842cabd</guid>
            <category><![CDATA[pip]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[python-environment]]></category>
            <category><![CDATA[venv]]></category>
            <dc:creator><![CDATA[Eric Flynn]]></dc:creator>
            <pubDate>Fri, 03 Jun 2022 16:25:23 GMT</pubDate>
            <atom:updated>2022-06-03T16:26:47.543Z</atom:updated>
            <content:encoded><![CDATA[<h4><a href="https://medium.com/python-environments">Python Environments</a></h4><h4>A Quick-Start Guide To Using venv For Python Environment Management</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Z_s9r3nAOCF_1qwo" /></figure><h3>Introduction</h3><p>The goal of this article is to provide you with a concise explanation of what venv is and then give you some real code examples on how to begin utilizing it for your Python environment management. Note: This article will assume you already have a working knowledge of Python/pip, and a basic understanding of what a Python environment is. If not, I encourage you to read <a href="https://medium.com/python-environments/managing-python-environments-part-1-dcdc53f20cb4">Managing Python Environments With Anaconda: Part 1</a>, where I give basic explanations of Python environments and pip.</p><h3>What Is venv?</h3><blockquote>The venv module provides support for creating lightweight “virtual environments” with their own site directories, optionally isolated from system site directories.</blockquote><blockquote>-python.org</blockquote><p>venv is the <strong>default virtual environment manager for Python 3</strong> and is automatically included in the Python standard library. This means that when you install Python on your machine, venv will come with it.</p><h3>Structure Of A Virtual Environment</h3><p>Before diving into our code examples of venv utilization, it is helpful to explain the overall structure of an environment created with venv. Upon initiation of a new virtual environment, venv will create a directory on your machine. This directory is where the virtual environment lives. The typical structure of a venv virtual environment is as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bF5SjN8F4kBKRqdSR2utfg.png" /><figcaption>Directory Structure of a venv</figcaption></figure><p>As you can see above, there are four main directories contained in our virtual environment. These are all created automatically by venv upon environment initiation.</p><ol><li><strong>bin : </strong>Contains binary executable files that are utilized in your environment. These are used to activate your environment, execute Python code and much more.</li><li><strong>include : </strong>Contains C headers that compile Python packages</li><li><strong>lib : </strong>Contains site-packages folder which is where the Python standard library and any pip-installed packages will be stored.</li><li><strong>pyvenv.cfg : </strong>Configuration file that stores basic environment information.</li></ol><h3>venv Commands</h3><p>In this section I will list out the most common commands used for different environment management applications.</p><h4>Creating An Environment</h4><p>The following command will create a Python virtual environment in the location that you specify. If no location is specified, then it will install in your current working directory. (Just be sure to still include a name for your environment) One thing to note is that whatever version of Python you issue the command with will be the version that the environment runs with. To specify your Python version, you must first be sure to have the desired version installed and then just simply replace “python3” with “python 3.10&quot;</p><pre>python3 -m venv path/to/new/virtual/environment</pre><h4>Activating An Environment</h4><p>Depending on your os, there may be a different commands for activating the environment. Shown below are commands for several different shells.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*I12A_yqsQLdalYpkS-EvsQ.png" /><figcaption>Activating a venv with different shells</figcaption></figure><p>Here is how to activate with a standard mac os shell. To deactivate simply type “deactivate”.</p><pre>source venv_name/bin/activate</pre><h4>Installing Packages</h4><p>Packages in venv are installed with pip, which will be automatically included when you initiate your environment. <strong>Be sure to activate your environment before installing any packages.</strong></p><pre>pip install &quot;package&quot;=&quot;version&quot;</pre><h4>“Freezing” Your Environment</h4><p>Freezing an environment is the first step you must take before being able to recreate it on a new machine. This creates a snapshot of your current pip-installed packages on your active environment and then saves it to a file typically named “requirements.txt”. This file will be automatically created in your current directory, but you can add in a path if you would like it to be stored somewhere else.</p><pre>pip freeze &gt; requirements.txt</pre><h4>Recreating Your Environment</h4><p>Once you have a requirements.txt file, you can share this via email or however you typically share files. To recreate this environment on a new machine, first save the file into your projects working directory. <strong>You must first create and activate a new virtual environment</strong>. Then, you can issue the following command to copy over all of your packages from the requirements.txt file.</p><pre>pip install -r requirements.txt</pre><h3>Full Code Example</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8MGcKYk_58-OKoNUTwbynA.png" /></figure><h4>Explanation</h4><ol><li>Create working directory for example (environments)</li><li>Create and activate test_env_1</li><li>Install pandas into test_env_1</li><li>Pip freeze environment and then deactivate test_env_1</li><li>Initiate new environment (test_env_2) and then activate it</li><li>Install requirements.txt file onto test_env_2</li><li>Verify that pandas as properly installed by importing</li></ol><h3>Conclusion</h3><p>In conclusion, venv is a simple and lightweight solution for Python environment management. It comes preinstalled in the Python standard library and each virtual environment created with venv will come with everything you need to begin writing Python scripts. Environments also come with pip included so you can install and manage third party packages with ease.</p><p>I hope that this article provided you with a good introduction to venv. Please comment with any questions or disagreements. Thank you for reading! 👋</p><h3>References:</h3><p>[Image] <a href="https://www.pexels.com/search/relax/">https://www.pexels.com/search/relax/</a></p><p>[1]<a href="https://docs.python.org/3/library/venv.html">https://docs.python.org/3/library/venv.html</a></p><p>[2]<a href="https://zetcode.com/python/venv/#:~:text=The%20pyvenv.,the%20version%20of%20the%20interpreter">https://zetcode.com/python/venv/#:~:text=The%20pyvenv.,the%20version%20of%20the%20interpreter</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=42205842cabd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/python-environments/learn-python-venv-in-under-5-minutes-42205842cabd">Learn Python venv In Under 5 Minutes</a> was originally published in <a href="https://medium.com/python-environments">Python Environments</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Managing Python Environments With Anaconda: Part 2]]></title>
            <link>https://medium.com/python-environments/managing-python-environments-with-anaconda-part-2-bdaa215e2d16?source=rss----e25aae99fe98---4</link>
            <guid isPermaLink="false">https://medium.com/p/bdaa215e2d16</guid>
            <category><![CDATA[environment-variables]]></category>
            <category><![CDATA[python-environment]]></category>
            <category><![CDATA[dependency-management]]></category>
            <category><![CDATA[pip]]></category>
            <category><![CDATA[conda]]></category>
            <dc:creator><![CDATA[Eric Flynn]]></dc:creator>
            <pubDate>Tue, 31 May 2022 12:33:32 GMT</pubDate>
            <atom:updated>2022-05-31T12:32:57.663Z</atom:updated>
            <content:encoded><![CDATA[<h4><a href="https://medium.com/python-environments">Python Environments</a></h4><h4>A Collection of Advanced Methodologies</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/494/1*jDHRAqICe4x_MJq23ubAHg.png" /><figcaption>Source: <a href="https://docs.conda.io/en/latest/">https://docs.conda.io/en/latest/</a></figcaption></figure><h3>Introduction</h3><p>This article is part two in a series dedicated to managing Python environments with Anaconda. This article will assume the reader already has a solid conceptual understanding of what a Python environment is and how to set one up using conda. If not, I highly recommend you to read part one which can be found <a href="https://medium.com/python-environments/managing-python-environments-part-1-dcdc53f20cb4">here</a>.</p><p>In this article I will first explain the concept of dependencies and then how to prevent and fix the notorious “dependency hell”. Next, I’ll cover the easiest way to export your current environment and then load it on a new machine. Lastly, the concept of environment variables will be introduced and you will learn an effective method of storing and retrieving sensitive information in Python with the help of conda.</p><h3>What is a Dependency?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/479/1*xwuNrgyPwFDv-AIEmeCfwQ.png" /></figure><p>Any discussion regarding Python environments would be incomplete without mentioning dependencies. “Dependencies are all of the software components required by your project in order for it to work as intended and avoid runtime errors.”[2] These “software components” are packages that are used as a building block of another package. For example, in order for pandas to operate it must utilize numpy arrays. In this sense, pandas depends on numpy and cannot work without it. This is why when you install pandas into an environment, numpy will automatically be installed with it.</p><h4>Managing Dependencies</h4><p>One common issue you will run into when combining multiple third party packages in one environment is a dependency conflict. For example sake, consider three packages: A, B and C. Let’s say B depends on C_1.3 however A depends on C_1.4. Since both A and B both depend on different versions of C, we run into what is called a transitive dependency conflict.</p><p>To check which dependencies a given package has with pip type pip show “package&quot;</p><pre>(myenv) ericflynn@Erics-MacBook-Air ~ % pip show pandas</pre><pre>Name: pandas</pre><pre>Version: 1.4.2</pre><pre>Summary: Powerful data structures for data analysis, time series, and statistics</pre><pre>Home-page: <a href="https://pandas.pydata.org/">https://pandas.pydata.org</a></pre><pre>Author: The Pandas Development Team</pre><pre>Author-email: pandas-dev@python.org</pre><pre>License: BSD-3-Clause</pre><pre>Location: /Users/ericflynn/opt/anaconda3/envs/myenv/lib/python3.10/site-packages</pre><pre>Requires: python-dateutil, numpy, pytz</pre><pre>Required-by:</pre><p>As you can see above, the output provides you with relevant information pertaining to the package. In the “Requires” field we can view the package dependencies. To perform the same thing with conda, we can use conda search &quot;package&quot; --info The response for conda will include all possible distribution channels available on Anaconda repository so be prepared. Below, I show the result from only one channel. We can view details pertaining to the specific download channel, and package information regarding size and dependencies. One thing I like better about the conda method is that it also shows the version of each dependency. (numpy &gt;=1.19.2,&lt;2.0a0)</p><pre>(myenv) ericflynn@Erics-MacBook-Air ~ % conda search pandas --info</pre><pre>pandas 1.4.2 py39he9d5cce_0</pre><pre>---------------------------</pre><pre>file name   : pandas-1.4.2-py39he9d5cce_0.conda</pre><pre>name        : pandas</pre><pre>version     : 1.4.2</pre><pre>build       : py39he9d5cce_0</pre><pre>build number: 0</pre><pre>size        : 9.4 MB</pre><pre>license     : BSD-3-Clause</pre><pre>subdir      : osx-64</pre><pre>url         : <a href="https://repo.anaconda.com/pkgs/main/osx-64/pandas-1.4.2-py39he9d5cce_0.conda">https://repo.anaconda.com/pkgs/main/osx-64/pandas-1.4.2-py39he9d5cce_0.conda</a></pre><pre>md5         : 9513b1735fc6feabfb647c545a5be53a</pre><pre>timestamp   : 2022-04-22 10:21:15 UTC</pre><pre>dependencies:</pre><pre>- bottleneck &gt;=1.3.1</pre><pre>- libcxx &gt;=12.0.0</pre><pre>- numexpr &gt;=2.7.1</pre><pre>- numpy &gt;=1.19.2,&lt;2.0a0</pre><pre>- python &gt;=3.9,&lt;3.10.0a0</pre><pre>- python-dateutil &gt;=2.8.1</pre><pre>- pytz &gt;=2020.1</pre><h4>Avoiding Dependency Conflicts</h4><p>Eventually, you are bound to run into a dependency conflict, and you may even find yourself in “dependency hell”. This is when you are stuck with a complex multi-layered dependency conflict. Conda does have a built in method for preventing dependency conflicts before they occur, but if you are using pip in conjunction with conda then you may still be prone to this issue. Here are some best-practice tips for managing dependencies and avoiding dependency hell.</p><ol><li>Understand why the issue is occurring</li><li>Isolate each project in its own environment</li><li>Install all packages at the same time</li><li>Stick to only conda packages</li><li>Degrade packages to resolve if possible</li></ol><h4>Modifying Package Versions</h4><p>Sometimes if you find yourself in a minor dependency conflict you may only need to modify one package to a slightly older version and this can fix your issue. Say for example, that a given package we just installed can only run with ≤ Python 3.9, but we’re currently using Python 3.10. To degrade a package on conda we simply just re-install it with the new version:</p><pre>conda install &quot;package&quot;=&quot;version&quot;</pre><p>Below, we will first issue conda list to verify which version of Python this environment is currently using. Then we can issue our conda install command specifying our desired Python version. Conda will show you which new packages will be downloaded and which downgrades will occur. Take note that Python is not the only package that was modified from this command. Since those other packages depend on Python, they must also be downgraded in order avoid creating any new dependency conflicts.</p><pre>(myenv) ericflynn@Erics-MacBook-Air envs_to_share % conda list</pre><pre># packages in environment at /Users/ericflynn/opt/anaconda3/envs/myenv:</pre><pre>...</pre><pre>python                    3.10.4               hdfd78df_0</pre><pre>...</pre><pre>(myenv) ericflynn@Erics-MacBook-Air envs_to_share % conda install python=3.9</pre><pre>...</pre><pre>The following packages will be downloaded:</pre><pre>package                    |            build</pre><pre>---------------------------|-----------------</pre><pre>certifi-2022.5.18.1        |   py39hecd8cb5_0         148 KB</pre><pre>python-3.9.12              |       hdfd78df_0        10.2 MB</pre><pre>setuptools-61.2.0          |   py39hecd8cb5_0        1012 KB</pre><pre>------------------------------------------------------------</pre><pre>Total:        11.4 MB</pre><pre>The following packages will be DOWNGRADED:</pre><pre>certifi                       2022.5.18.1-py310hecd8cb5_0 --&gt; 2022.5.18.1-py39hecd8cb5_0</pre><pre>pip                                21.2.4-py310hecd8cb5_0 --&gt; 21.2.4-py39hecd8cb5_0</pre><pre>python                                  3.10.4-hdfd78df_0 --&gt; 3.9.12-hdfd78df_0</pre><pre>setuptools                         61.2.0-py310hecd8cb5_0 --&gt; 61.2.0-py39hecd8cb5_0</pre><p>The same process can be done with pip using:</p><pre>pip install &quot;package&quot;==&quot;version&quot;</pre><p>The key difference here is “==” opposed to “=”. See below for how I downgraded pandas from 1.4.2 to 1.4.1. The process is very similar to conda, however there is no (y/n) prompt so be sure to type the correct version.</p><pre>(myenv) ericflynn@Erics-MacBook-Air envs_to_share % pip install pandas==1.4.1</pre><pre>Collecting pandas==1.4.1</pre><pre>...</pre><pre>Attempting uninstall: pandas</pre><pre>Found existing installation: pandas 1.4.2</pre><pre>Uninstalling pandas-1.4.2:</pre><pre>Successfully uninstalled pandas-1.4.2</pre><pre>Successfully installed pandas-1.4.1</pre><h3>How to Share a Conda Environment</h3><p>Python and its various package repositories are constantly being updated. Sometimes with a big enough change (Like a brand new version of Python), scripts that worked perfectly fine may not work in a new environment that contains different versions of packages. It can be difficult and time consuming to recreate an environment containing each package and version you utilize. Luckily, conda includes commands for this exact purpose! With conda you can create a snapshot of a given environment and then save it as a .yml file. These files can then be loaded up to a new machine, and used to run your scripts. This guarantees that each package will have the exact same version and will also maintain all dependency requirements.</p><h4>Exporting An Environment to .yml</h4><pre>conda env export &gt; &quot;myenv&quot;.yml</pre><p>This command will create a .yml file in your current directory which is a snapshot of the environment you have activated. (Note: You must be sure to first activate the desired environment) In the snippet below, first I created a new directory to store my environment in with mkdir. Next, I changed my directory to that new folder and issued the export command. Finally, we can verify that the file was created with ls . Don’t forget that wherever your current directory is set to when you issue conda env export, is where the .yml file will be stored.</p><pre>(base) ericflynn@Erics-MacBook-Air desktop % conda activate myenv</pre><pre>(myenv) ericflynn@Erics-MacBook-Air desktop % mkdir envs_to_share</pre><pre>(myenv) ericflynn@Erics-MacBook-Air desktop % cd envs_to_share</pre><pre>(myenv) ericflynn@Erics-MacBook-Air envs_to_share % conda env export &gt; myenv.yml</pre><pre>(myenv) ericflynn@Erics-MacBook-Air envs_to_share % ls</pre><pre>myenv.yml</pre><p>Now that we have our environment recreated as a .yml file, we can share this file via email or however you traditionally share documents. Here is what the file itself looks like if you open it in a text editor:</p><pre>name: myenv<br>channels:<br>  - defaults<br>dependencies:<br>  - bzip2=1.0.8=h1de35cc_0<br>  - ca-certificates=2022.4.26=hecd8cb5_0<br>  - certifi=2022.5.18.1=py310hecd8cb5_0<br>  - libcxx=12.0.0=h2f01273_0<br>  - libffi=3.3=hb1e8313_2<br>  - ncurses=6.3=hca72f7f_2<br>  - openssl=1.1.1o=hca72f7f_0<br>  - pip=21.2.4=py310hecd8cb5_0<br>  - python=3.10.4=hdfd78df_0<br>  - readline=8.1.2=hca72f7f_1<br>  - setuptools=61.2.0=py310hecd8cb5_0<br>  - sqlite=3.38.3=h707629a_0<br>  - tk=8.6.11=h3fd3227_1<br>  - tzdata=2022a=hda174b7_0<br>  - wheel=0.37.1=pyhd3eb1b0_0<br>  - xz=5.2.5=hca72f7f_1<br>  - zlib=1.2.12=h4dc903c_2<br>  - pip:<br>    - numpy==1.22.4<br>    - pandas==1.4.2<br>    - python-dateutil==2.8.2<br>    - pytz==2022.1<br>    - six==1.16.0<br>variables:<br>prefix: /Users/ericflynn/opt/anaconda3/envs/myenv</pre><p>Now lets create a new environment from this .yml file. Since we are on the same machine, we must first delete the environment and then verify it is deleted.</p><pre>(base) ericflynn@Erics-MacBook-Air ~ % conda remove --name myenv --all</pre><pre>...</pre><pre>(base) ericflynn@Erics-MacBook-Air ~ % conda activate myenv</pre><pre>Could not find conda environment: myenv</pre><pre>You can list all discoverable environments with `conda info --envs`.</pre><h4>Creating An Environment From .yml</h4><p>Now that the environment is deleted, we can attempt to recreate it from our saved .yml file. First, you must navigate to the folder where you have saved the .yml file with cd . Then we can issue the following command:</p><pre>conda anv create -f &quot;myenv&quot;.yml</pre><p>This will create a brand new environment based on the .yml file that we created previously. Just like before, make sure to be in the same directory that contains the .yml file you are creating from before issuing the create command.</p><pre>(base) ericflynn@Erics-MacBook-Air ~ % cd desktop/envs_to_share</pre><pre>(base) ericflynn@Erics-MacBook-Air envs_to_share % conda env create -f myenv.yml</pre><pre>Collecting package metadata (repodata.json): done</pre><pre>...</pre><pre>Successfully installed numpy-1.22.4 pandas-1.4.2 python-dateutil-2.8.2 pytz-2022.1 six-1.16.0</pre><pre>done</pre><p>We can validate that it was installed by activating and listing out our packages. Note: Packages that we installed with pip are still installed on our recreated environment! (We can see by the “pypi” build channel)</p><pre>(base) ericflynn@Erics-MacBook-Air envs_to_share % conda activate myenv</pre><pre>(myenv) ericflynn@Erics-MacBook-Air envs_to_share % conda list</pre><pre># packages in environment at /Users/ericflynn/opt/anaconda3/envs/myenv:</pre><pre>#</pre><pre># Name                    Version                   Build  Channel</pre><pre>bzip2                     1.0.8                h1de35cc_0</pre><pre>ca-certificates           2022.4.26            hecd8cb5_0</pre><pre>certifi                   2022.5.18.1     py310hecd8cb5_0</pre><pre>libcxx                    12.0.0               h2f01273_0</pre><pre>libffi                    3.3                  hb1e8313_2</pre><pre>ncurses                   6.3                  hca72f7f_2</pre><pre>numpy                     1.22.4                   pypi_0    pypi</pre><pre>openssl                   1.1.1o               hca72f7f_0</pre><pre>pandas                    1.4.2                    pypi_0    pypi</pre><pre>pip                       21.2.4          py310hecd8cb5_0</pre><pre>python                    3.10.4               hdfd78df_0</pre><pre>python-dateutil           2.8.2                    pypi_0    pypi</pre><pre>pytz                      2022.1                   pypi_0    pypi</pre><pre>readline                  8.1.2                hca72f7f_1</pre><pre>setuptools                61.2.0          py310hecd8cb5_0</pre><pre>six                       1.16.0                   pypi_0    pypi</pre><pre>sqlite                    3.38.3               h707629a_0</pre><pre>tk                        8.6.11               h3fd3227_1</pre><pre>tzdata                    2022a                hda174b7_0</pre><pre>wheel                     0.37.1             pyhd3eb1b0_0</pre><pre>xz                        5.2.5                hca72f7f_1</pre><pre>zlib                      1.2.12               h4dc903c_2</pre><h4>Cloning Environments</h4><p>Another way of recreating environments is using conda’s --clone feature. This can be useful when you would like to add a new package to your environment, but you don’t want to run the risk of dependency issues. A clone can act as a great testing ground so you don’t mess up your perfectly integrated environment. In the command, “CloneName” will be the new name we set for the environment and “myenv” is the environment which we are cloning. We will skip the detailed code example for this one because it is pretty self explanatory.</p><pre>conda create --name &quot;CloneName&quot; --clone &quot;myenv&quot;</pre><h3>Environment Variables</h3><p>Environment variables are key-value pairs that you can manually set from within a given conda environment. These variables can then be accessed from any Python script when that environment is activated. One application for environment variables is for storing passwords and secret API keys, since you can call the variables from within Python and don’t have to explicitly type them out in your code. To set a new environment variable, first activate your desired environment and then use the following:</p><pre>conda env config vars set &quot;key&quot;=&quot;value&quot;</pre><p>Here we will set our variable key “secret_api_key” to be equal to “123abc”. Make sure to re-activate your environment once the variable is set with conda activate &quot;myenv&quot; . You can then verify that the variable was set with conda env config vars list . You may check the value of a single environment variable with echo $&quot;var_name&quot; .</p><pre>(base) ericflynn@Erics-MacBook-Air ~ % conda activate myenv</pre><pre>(myenv) ericflynn@Erics-MacBook-Air ~ % conda env config vars set secret_api_key=123abc</pre><pre>To make your changes take effect please reactivate your environment</pre><pre>(myenv) ericflynn@Erics-MacBook-Air ~ % conda activate myenv</pre><pre>(myenv) ericflynn@Erics-MacBook-Air ~ % conda env config vars list</pre><pre>secret_api_key = 123abc</pre><pre>(myenv) ericflynn@Erics-MacBook-Air ~ % echo $secret_api_key</pre><pre>123abc</pre><p>Now that we have our environment variable set, lets launch Python and call it.</p><pre>(myenv) ericflynn@Erics-MacBook-Air ~ % python</pre><pre>Python 3.10.4 (main, Mar 31 2022, 03:38:35) [Clang 12.0.0 ] on darwin</pre><pre>Type &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.</pre><pre>&gt;&gt;&gt; print(secret_api_key)</pre><pre>Traceback (most recent call last):</pre><pre>File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;</pre><pre>NameError: name &#39;secret_api_key&#39; is not defined</pre><p>Hmmm…. so Python says that my “secret_api_key” isn’t defined but I clearly just defined it and even just verified that it is there within conda. The reason that Python cannot call my environment variable from just its key is because environment variables don’t behave like normal variables. They get stored in your system and not Python. So in order to access these variables from Python, we must utilize a package that can integrate Python and our operating system.</p><h4>Environment Variables and OS</h4><p>The os package comes pre-installed by default with any Python installation, and it provides numerous classes for interacting with a machine’s operating system. For this article, we will primarily be concerned with one class:</p><pre>os.environ</pre><p>Let’s continue where we left off before, after being denied access to our “secret_api_key” within Python. First, we want to import our os package and then we will print our os.environ object.</p><pre>&gt;&gt;&gt; import os</pre><pre>&gt;&gt;&gt; print(os.environ)</pre><p>This object will be a dictionary that contains all of the environment variables stored on this environment within your machine. In that dictionary, you should be able to find the “secret_api_key” that we set up before along with multiple other key:value pairs. Environment variables are utilized for much more than just storing of sensitive information. Conda uses them by default to facilitate internal operations that occur during environment management. In the code below, we’re able to fetch certain values from our environment variable dictionary by using .get(&quot;Key&quot;) .</p><pre>&gt;&gt;&gt; print(os.environ.get(&#39;CONDA_PREFIX&#39;))</pre><pre>/Users/ericflynn/opt/anaconda3/envs/myenv</pre><p>‘CONDA_PREFIX’ is the path to where our current environment resides. This is an integral piece of information that conda must continuously call on to perform different operations. Without these environment variables, you would have to manually direct the environment where to install new packages by specifying the path explicitly each time. Instead, conda can just check its prefix path and automatically install any new packages there.</p><p>Below I will show how we can access our previously created “secret_api_key” environment variable. If you have launched a new instance, be sure to have the correct environment activated and don’t forget to import os before trying the below code.</p><pre>&gt;&gt;&gt; Key = os.environ.get(&#39;secret_api_key&#39;)</pre><pre>&gt;&gt;&gt; print(Key)</pre><pre>123abc</pre><p>Congratulations, you can now utilize your manually defined environment variable from within Python! We can even re-export our environment as a .yml file and we can see the “variables:” section now contains our secret api key.</p><pre>name: myenv<br>channels:<br>  - defaults<br>dependencies:<br>  - bzip2=1.0.8=h1de35cc_0<br>  - ca-certificates=2022.4.26=hecd8cb5_0<br>  - certifi=2022.5.18.1=py310hecd8cb5_0<br>  - libcxx=12.0.0=h2f01273_0<br>  - libffi=3.3=hb1e8313_2<br>  - ncurses=6.3=hca72f7f_2<br>  - openssl=1.1.1o=hca72f7f_0<br>  - pip=21.2.4=py310hecd8cb5_0<br>  - python=3.10.4=hdfd78df_0<br>  - readline=8.1.2=hca72f7f_1<br>  - setuptools=61.2.0=py310hecd8cb5_0<br>  - sqlite=3.38.3=h707629a_0<br>  - tk=8.6.11=h3fd3227_1<br>  - tzdata=2022a=hda174b7_0<br>  - wheel=0.37.1=pyhd3eb1b0_0<br>  - xz=5.2.5=hca72f7f_1<br>  - zlib=1.2.12=h4dc903c_2<br>  - pip:<br>    - numpy==1.22.4<br>    - pandas==1.4.2<br>    - python-dateutil==2.8.2<br>    - pytz==2022.1<br>    - six==1.16.0<br>variables:<br>  secret_api_key: 123abc<br>prefix: /Users/ericflynn/opt/anaconda3/envs/myenv</pre><p>If you would like to remove an environment variable you can use conda env config vars unset &quot;var_name&quot; . Be sure to re-activate your environment after.</p><h3>Conclusion</h3><p>Conda is an extremely powerful tool and mastering it has helped me in taking my Python programming to the next level. I can now spend much less time stressing over dependency conflicts and “module not found” errors and spend more time actually writing valuable code.</p><p>If there is anything that you are confused on or disagree with please leave a comment so we can discuss. Thank you for reading and I hope you learned something!😄</p><h3>References:</h3><p>[1] <a href="https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html">https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html</a></p><p>[2] <a href="https://www.activestate.com/resources/quick-reads/python-dependencies-everything-you-need-to-know/#:~:text=Dependencies%20are%20all%20of%20the,to%20web%20development%2C%20and%20more">https://www.activestate.com/resources/quick-reads/python-dependencies-everything-you-need-to-know/#:~:text=Dependencies%20are%20all%20of%20the,to%20web%20development%2C%20and%20more</a>.</p><p>[3] <a href="https://www.nylas.com/blog/making-use-of-environment-variables-in-python/">https://www.nylas.com/blog/making-use-of-environment-variables-in-python/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bdaa215e2d16" width="1" height="1" alt=""><hr><p><a href="https://medium.com/python-environments/managing-python-environments-with-anaconda-part-2-bdaa215e2d16">Managing Python Environments With Anaconda: Part 2</a> was originally published in <a href="https://medium.com/python-environments">Python Environments</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Managing Python Environments With Anaconda: Part 1]]></title>
            <link>https://medium.com/python-environments/managing-python-environments-part-1-dcdc53f20cb4?source=rss----e25aae99fe98---4</link>
            <guid isPermaLink="false">https://medium.com/p/dcdc53f20cb4</guid>
            <category><![CDATA[conda]]></category>
            <category><![CDATA[python-environment]]></category>
            <category><![CDATA[pip]]></category>
            <category><![CDATA[anaconda]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Eric Flynn]]></dc:creator>
            <pubDate>Tue, 17 May 2022 15:38:49 GMT</pubDate>
            <atom:updated>2022-05-21T18:29:53.021Z</atom:updated>
            <content:encoded><![CDATA[<h4><a href="https://medium.com/python-environments">Python Environments</a></h4><h4>A Comprehensive Starters Guide To Anaconda</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*sIJVYIY0ereW35Am.png" /><figcaption>Source: <a href="https://www.anaconda.com/">https://www.anaconda.com/</a></figcaption></figure><h3>Introduction</h3><p>As you work on more advanced Python projects, you will eventually need to install third party packages. These packages are collections of .py files that contain predefined functions and classes that can make your life a whole lot easier when writing Python code. One way to organize these packages is through the use of “environments”. Managing Python environments can be one of the most difficult concepts for a beginner to grasp. If you’re like me, then you probably just pip-installed every package into your base environment. This may work just fine for a while, but eventually you are bound to run into issues.</p><p>This article is part one of a series dedicated to explaining different methodologies for managing Python environments. The topic of this writing will be the <a href="https://www.anaconda.com/">Anaconda Distribution</a>, the world’s most popular open-source Python distribution platform<em>.</em></p><h3>What is a Python Environment?</h3><p>At the simplest level, <strong>a Python environment is a folder </strong>located on your machine that contains everything you will need to run your Python scripts. Typically, this includes a Python interpreter, and any third party packages that are to be used within the environment.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*3yzZUHGC0euywMihMR5bBg.png" /><figcaption>Multiple Python Environments (<a href="https://www.dataquest.io/blog/a-complete-guide-to-python-virtual-environments/">dataquest.io</a>)</figcaption></figure><p>As you can see in the picture above, each environment may have a specific installation of Python, and also different versions of each package. This is especially useful when working on multiple projects on the same machine, that must each utilize different versions of the same packages. Environments can also facilitate sharing of scripts with co-workers. Instead of having to install each and every package to run your code, a colleague can just import the entire environment and run your script with ease. (Refer to part two for a specific example)</p><h4>The Anaconda Distribution</h4><p>Anaconda is marketed as <em>“the easiest way to perform Python/R data science and machine learning on a single machine”.</em></p><p>The Anaconda distribution is composed of three main components.</p><p><strong>1. Anaconda Repository:</strong> Collection of commonly used Python/R packages. Similar to the PyPI Index.</p><blockquote>“…[The Anaconda repository] features over 8,000 open-source data science and machine learning packages, Anaconda-built and compiled for all major operating systems and architectures.”</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/575/1*JYaYdQxLkQBefY1HBosjFQ.png" /><figcaption>Anaconda Repository (<a href="https://www.anaconda.com/">https://www.anaconda.com/</a>)</figcaption></figure><p><strong>2. Conda: </strong>Command line environment manager compatible with all major operating systems.</p><blockquote>“Conda is an open-source package and environment management system that runs on Windows, macOS, and Linux. Conda quickly installs, runs, and updates packages and their dependencies. It also easily creates, saves, loads, and switches between environments on your local computer. It was created for Python programs, but it can package and distribute software for any language.”</blockquote><p><strong>3: Anaconda Navigator:</strong> Desktop GUI to manage environments without command line.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*UvZkS9LEtT8H9SLK1M1ApA.png" /><figcaption>Anaconda Navigator (<a href="https://www.anaconda.com/">https://www.anaconda.com/</a>)</figcaption></figure><h4>Anaconda Installation</h4><p>The download process for Anaconda is fairy simple, and Anaconda maintains excellent documentation so click <a href="https://docs.anaconda.com/anaconda/install/">here</a> to access the detailed guide based on your operating system. Once installed, navigate to your Anaconda 3 folder and explore its contents. Understanding how the Anaconda directories are stored will help drastically in conceptualizing how Python environments work.</p><h4>A Quick Note On pip</h4><p>By default, each new Anaconda environment that includes Python will also come with <a href="https://pip.pypa.io/en/stable/">pip</a> pre-installed. Its documentation sums up the capabilities elegantly.</p><blockquote>“pip is the <a href="https://packaging.python.org/guides/tool-recommendations/">package installer for Python</a>. You can use it to install packages from the <a href="https://pypi.org/">Python Package Index</a> and other indexes.”</blockquote><p>Pip truly is one of my favorite Python package installers. It works great in conjunction with Anaconda, and can allow you to install packages that may not be available on the Anaconda repository. One thing to note, however is that working with pip and Conda in conjunction can lead to potential issues. The rule of thumb is <strong>Conda before pip</strong>.</p><blockquote>Issues may arise when using pip and conda together. When combining conda and pip, it is best to use an isolated conda environment. Only after conda has been used to install as many packages as possible should pip be used to install any remaining software.</blockquote><h3>Using Anaconda To Manage A Python Environment</h3><p>For this tutorial, I am going to be using Conda for all of my environment management needs. If you can grasp how to use Conda from the command line, then utilizing the Anaconda Navigator GUI will be a breeze. I use Conda exclusively for environment management because it is fast and simple. (And I enjoy command line programming because it reminds me of the matrix 😎)</p><h4>Setting Up Conda</h4><p>Depending on what OS you are operating on, there may be different ways of setting up Conda for use. For this example, I will go over how to set up on Windows, but I will provide a resource for MacOS <a href="https://docs.conda.io/projects/conda/en/latest/user-guide/install/macos.html">here</a>. On Windows, Anaconda is nice enough to come pre-installed with Anaconda Prompt, which is an isolated command line dedicated to running Conda commands. If you still prefer to run Conda from the standard Windows command line, you must first add it to your environment path.</p><ol><li>Type “env” into the Windows search and click the first result</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/1*Ir8rb9WphsPsvuQKvMoZAA.png" /></figure><p>2. Navigate to “advanced” from the tab, and click “Environment Variables…”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/413/1*0wPNEqw9BxcrNYxjo3SNbQ.png" /></figure><p>3. Click on your “Path” variable and then edit</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/623/1*-LqRN_9XFF__kCLbcnZjdg.png" /></figure><p>4. Add new path to the condabin folder within your anaconda3 directory. (“wherever_you_installed\anaconda3\condabin”)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/527/1*Y_OVbq8el_N_O1_vB1hNQg.png" /></figure><p>5. Congrats! You should now be able to issue Conda commands from the Windows command line. Test it out by typing “Conda env list” in a new cmd window. You should see your base environment.</p><h3>Conda Commands</h3><p>Note: All of these commands will work by default on Anaconda prompt. In order to use in command prompt/terminal, be sure to first enable Conda for your specific operating system.</p><p>There are very few Conda commands that you will use on a day to day basis. Here I will show you the 5 top commands and give a brief explanation.</p><pre>conda activate &quot;env&quot;</pre><p>This is the most basic of all Conda commands, and it is used to activate a given environment. For this example, since we haven’t created any environments yet, I will activate “base”. Base environment is the standard environment that comes with Anaconda upon installation. As you can see, once activated you will see (base) next to your curser. For those using Anaconda prompt, you will start in your base environment by default so you can skip this one.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*VRcmXNVGTgUAKEmkFoN0Aw.png" /></figure><pre>conda env list</pre><p>“conda env list” is used to show all the current Anaconda environments you have installed on your machine. This can be utilized without first activating an environment. As you can see, I have many different Conda environments. This is because I chose to isolate each project to an environment. If you haven’t yet created any Conda environments, then you will only see your base environment here. On the right hand side, you can see the path to each environment directory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/1*34F9LcjSFgiMRXZCksIB2g.png" /></figure><pre>conda create -n &quot;Name_Of_Env&quot; &quot;Package&quot;=&quot;Version&quot;</pre><p>The “conda create” is used to create a brand new Conda environment for use. You will just have to input the desired name of the environment and any packages that you would like to have installed upon initial setup. For this example, we will create a new environment “myenv” with python 3.10 installed on it. By typing “y” + enter you will initiate the installation. Once the download is open, try running “conda env list” again to see it now added into your environment list.</p><p>Note: to add multiple packages to initial installation, just separate them with spaces and follow the same “package=version” format.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*Y0e_cXRsflW3SHbqQbDr4Q.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*Z01phGP2k1NcDgrPEq-11A.png" /></figure><pre>conda list</pre><p>“conda list” is similar to “conda env list” however it is used to list out each individual package you have installed in a given environment. For this command, you must first activate your environment of choice.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*YuUTs707aMME4atLdB2vdg.png" /></figure><pre>conda remove --name &quot;Name_Of_Env&quot; --all</pre><p>“conda remove” is pretty self explanatory and allows you to delete a Conda environment. Make sure to deactivate any open environments before running this command.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/910/1*LNYU1YNcx5U6JB2yaGSkYw.png" /></figure><h3>Installing Packages</h3><p>Installing packages into an Anaconda environment is extremely easy. First, we will launch Python from our previously created environment and attempt to import pandas. In order to launch Python, you must first change your directory to the location of where your Python interpreter is saved. Remember, Python environments are just folders that are stored locally on your machine. We must tell our computer which version of Python we want to load. To do this, we will cd (change directory) to the Python installation within our “myenv” environment folder. Recall from before, if you type “conda env list” you can see the location where each of your Conda environments are stored on your local machine. Locate your “myenv” folder, and navigate to it using cd. (Shown below). Then type “python” and you will see the curser change to &gt;&gt;&gt; . This means you are now operating in Python.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*9L0YfYxZQYH3zgeQui30ZQ.png" /></figure><p>If we try importing pandas at this point, we will get an error message because we haven’t yet downloaded the pandas package into “myenv”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/558/1*D8wRVFo-mzNC-UahSt9eqg.png" /></figure><p>To install pandas, open another cmd window and activate “myenv”. Once activated, we can either pip/conda install pandas. In this example, I used pip. The only difference is the location of the repository from which you download the package. (Either PyPi or Conda repository)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/936/1*kxNxhcWjR8FZ7wArYM3G7g.png" /></figure><p>As you can see, when we go back into Python and try to import pandas we now have no issue. We can even call on a pandas function to verify that it worked properly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/484/1*LWi1CG6TuToHWcs_zCzI2Q.png" /></figure><h3>Conclusion</h3><p>In conclusion, this article has explained the basics of what a Python environment is and why every Python programmer should be utilizing them. The Anaconda Distribution is an industry leader in Python environment management due to its simplicity, open-source nature, and extensive documentation. Conda is Anaconda’s command line environment manager. This article reviewed how to get started using Conda and then showed the top five Conda commands. These commands should be more than enough to get started and begin experimenting on your own with Anaconda environments. Thank you for reading and I hope that you now have a better understanding of Python environments and Anaconda! 👋</p><h3>References</h3><p><a href="https://www.anaconda.com/">https://www.anaconda.com/</a></p><p><a href="https://docs.conda.io/en/latest/">https://docs.conda.io/en/latest/</a></p><p><a href="https://pypi.org/">https://pypi.org/</a></p><p><a href="https://pip.pypa.io/en/stable/">https://pip.pypa.io/en/stable/</a></p><p><a href="https://www.dataquest.io/blog/a-complete-guide-to-python-virtual-environments/">https://www.dataquest.io/blog/a-complete-guide-to-python-virtual-environments/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dcdc53f20cb4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/python-environments/managing-python-environments-part-1-dcdc53f20cb4">Managing Python Environments With Anaconda: Part 1</a> was originally published in <a href="https://medium.com/python-environments">Python Environments</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>