<?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: Prabhu Pant</title>
    <description>The latest articles on DEV Community by Prabhu Pant (@prabhupant).</description>
    <link>https://dev.to/prabhupant</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%2F242056%2Fee0e1d17-1fd3-4e30-b432-e4e427d3ef05.jpeg</url>
      <title>DEV Community: Prabhu Pant</title>
      <link>https://dev.to/prabhupant</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prabhupant"/>
    <language>en</language>
    <item>
      <title>How virtual environment works in Python</title>
      <dc:creator>Prabhu Pant</dc:creator>
      <pubDate>Wed, 09 Oct 2019 16:42:41 +0000</pubDate>
      <link>https://dev.to/prabhupant/how-virtual-environment-works-in-python-50kg</link>
      <guid>https://dev.to/prabhupant/how-virtual-environment-works-in-python-50kg</guid>
      <description>&lt;p&gt;A virtual environment is a tool which is used to keep dependencies used by different projects separate by creating isolated Python virtual environment for them. This is one of the most important tool used while making a Python project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use virtual environment?
&lt;/h2&gt;

&lt;p&gt;Suppose you are building a web app that uses Flask 1.0.2. So you decide to system wide general installation of Flask by using &lt;code&gt;pip3 install flask=1.0.2&lt;/code&gt;. After installing Flask, you start working on the project and all of a sudden you need to make changes in some other project, or start a different project in parallel. But that project is based on Flask 1.1.0. Hence, your installed version of Flask won't be compatible for the second project.&lt;/p&gt;

&lt;p&gt;In this scenario, virtual environments play a crucial role. We create separate virtual environments for separate projects and install the required dependencies as per each project, hence separting the environment of projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  How virtual environment works
&lt;/h2&gt;

&lt;p&gt;When Python starts its interpreter, it searches for a site-specific directory where all the packages are located. The search commences from the parent directory of the Python executable file and continues going up towards the parent directories until it reaches the root directory. To determine if it's a site-specific directory, Python looks for the os.py module, which is a mandatory requirement by Python in order to work.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a virtual environment
&lt;/h2&gt;

&lt;p&gt;First install the &lt;code&gt;virtualenv&lt;/code&gt; package&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt-get &lt;span class="nb"&gt;install &lt;/span&gt;virtualenv
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;How go to the project directory and type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;python3 &lt;span class="nt"&gt;-m&lt;/span&gt; venv venv
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;ls
&lt;/span&gt;venv

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This will create a virtual environment called &lt;code&gt;venv&lt;/code&gt; and a directory of the same name. The name is specified by the third argument of the command &lt;code&gt;python3 -m venv venv&lt;/code&gt;. So changing it to &lt;code&gt;python3 -m venv virtual&lt;/code&gt; will create a virtual environment with the name &lt;code&gt;virtual&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To activate the virtual environment, type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;source &lt;/span&gt;venv/bin/activate
&lt;span class="o"&gt;(&lt;/span&gt;venv&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;venv&lt;/code&gt; will get prepended to the bash command prompt which indicates that you are inside the &lt;code&gt;venv&lt;/code&gt; virtual environment. To deactivate it, simply type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;(&lt;/span&gt;venv&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="nv"&gt;$ &lt;/span&gt;deactivate
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Under the hood of virtual environment
&lt;/h2&gt;

&lt;p&gt;Let's see what's under the &lt;code&gt;venv&lt;/code&gt; virtual environment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;tree &lt;span class="nt"&gt;-L&lt;/span&gt; 3
&lt;span class="nb"&gt;.&lt;/span&gt;
└── venv
    ├── bin
    │   ├── activate
    │   ├── activate.csh
    │   ├── activate.fish
    │   ├── easy_install
    │   ├── easy_install-3.5
    │   ├── pip
    │   ├── pip3
    │   ├── pip3.5
    │   ├── python -&amp;gt; python3
    │   └── python3 -&amp;gt; /usr/bin/python3
    ├── include
    ├── lib
    │   └── python3.5
    ├── lib64 -&amp;gt; lib
    ├── pyvenv.cfg
    └── share
        └── python-wheels

8 directories, 11 files

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;From above, we can conclude that the environmet was created by copying the Python binary to the local directory (/venv/bin/python).&lt;/p&gt;

&lt;p&gt;Now let's look at the environment variables i.e &lt;code&gt;$PATH&lt;/code&gt;. Before activating the virtual environment, the &lt;code&gt;$PATH&lt;/code&gt; variable points to the original location of the executable binaries but after activating the virtual environment, it changes the &lt;code&gt;$PATH&lt;/code&gt; variable in such a way that Python will point to our local version. So basically it prepends our local path to the &lt;code&gt;$PATH&lt;/code&gt; variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;prabhu@prabhu-pc:~/project&lt;span class="nv"&gt;$ &lt;/span&gt;which python
/usr/bin/python

prabhu@prabhu-pc:~/project&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;source &lt;/span&gt;venv/bin/activate
&lt;span class="o"&gt;(&lt;/span&gt;venv&lt;span class="o"&gt;)&lt;/span&gt; prabhu@prabhu-pc:~/project&lt;span class="nv"&gt;$ &lt;/span&gt;which python
/home/prabhu/project/venv/bin/python
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But copying the executable to the new local location is not a good idea. So in Python3, the &lt;code&gt;pyvenv.cfg&lt;/code&gt; file is used to created instead of Python binary and its modules and you can specify their location in this config file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;(&lt;/span&gt;venv&lt;span class="o"&gt;)&lt;/span&gt; prabhu@prabhu-pc:~/project/venv&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;cat &lt;/span&gt;pyvenv.cfg
home &lt;span class="o"&gt;=&lt;/span&gt; /usr/bin
include-system-site-packages &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;false
&lt;/span&gt;version &lt;span class="o"&gt;=&lt;/span&gt; 3.5.2
&lt;span class="o"&gt;(&lt;/span&gt;venv&lt;span class="o"&gt;)&lt;/span&gt; prabhu@prabhu-pc:~/project/venv&lt;span class="err"&gt;$&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>python</category>
      <category>venv</category>
      <category>linux</category>
    </item>
    <item>
      <title>I made a GitHub repo for data structures and algorithms in Python to help in interview prep</title>
      <dc:creator>Prabhu Pant</dc:creator>
      <pubDate>Wed, 09 Oct 2019 05:33:12 +0000</pubDate>
      <link>https://dev.to/prabhupant/i-made-a-github-repo-for-data-structures-and-algorithms-in-python-to-help-in-interview-prep-4dk8</link>
      <guid>https://dev.to/prabhupant/i-made-a-github-repo-for-data-structures-and-algorithms-in-python-to-help-in-interview-prep-4dk8</guid>
      <description>&lt;p&gt;Visit the repo here - &lt;a href="https://github.com/prabhupant/python-ds"&gt;https://github.com/prabhupant/python-ds&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have been using open source software in my daily life for over 2 years now. The open source community has helped me in my studies, interview preparations and in general to become a better software engineer. The community has given a lot to the world and because of open source, softwares and computers have become so pervasive in our daily lifes. This is the time for me to give something back to the community.&lt;/p&gt;

&lt;p&gt;While preparing for my interviews, I did many Leetcode, HackerRank and GeeksForGeeks problems in Python and felt that if there was a GitHub repo where I could find implementations of all data structures and algorithms, it would help many students. So I set out to compile all the problems I did in a single place. Hence I created the &lt;a href="https://github.com/prabhupant/python-ds"&gt;python-ds&lt;/a&gt; repo.&lt;/p&gt;

&lt;p&gt;Feel free to raise issues, file PR and contribute to this repo 😃&lt;/p&gt;

&lt;p&gt;And don’t forget to fork and star 😉&lt;/p&gt;

</description>
      <category>python</category>
      <category>contributorswanted</category>
      <category>hacktoberfest</category>
    </item>
  </channel>
</rss>
