<?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>Forem: Raghavendra Khare</title>
    <description>The latest articles on Forem by Raghavendra Khare (@raghavxk).</description>
    <link>https://forem.com/raghavxk</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%2F684750%2F5bd06e47-a5ca-49f1-a50d-64fb249ad0c0.jpg</url>
      <title>Forem: Raghavendra Khare</title>
      <link>https://forem.com/raghavxk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/raghavxk"/>
    <language>en</language>
    <item>
      <title>What are environment variables and how to manage them in Python?</title>
      <dc:creator>Raghavendra Khare</dc:creator>
      <pubDate>Fri, 04 Feb 2022 17:25:32 +0000</pubDate>
      <link>https://forem.com/raghavxk/what-are-environment-variables-and-how-to-manage-them-in-python-20kj</link>
      <guid>https://forem.com/raghavxk/what-are-environment-variables-and-how-to-manage-them-in-python-20kj</guid>
      <description>&lt;p&gt;As beginner software developers, we often come accross term "environment variables". In this blog post, I will try to explain what are environment variables and how you can manage them in your Python project dev environment. Let's get started.&lt;br&gt;
&lt;em&gt;Note: The blog follows Ubuntu workflow which can be easily replicated in MacOS/Windows with slight variations.&lt;/em&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  What are environment variables?
&lt;/h1&gt;

&lt;p&gt;A quick google search will land you up to this definition of environment variables from Wikipedia,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;An environment variable is a dynamic-named value that can affect the way running processes will behave on a computer. They are part of the environment in which a process runs.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Think of environment variables as system-wide variables which may store location or values of resources held by the system. Environment variables can be system defined as well as user defined too.&lt;/p&gt;

&lt;p&gt;If you are using MacOS/Linux open your terminal and run command,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ printenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which will quickly show you all environment variables present in your system. These are some resource values which are used by programs running on your system.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is the use of environment variables?
&lt;/h1&gt;

&lt;p&gt;While developing our projects, our projects can contain various confidential information which can not be made public to prevent integrity violation of our app. For example, if you have used Django you may know about secret key it uses for signing session cookies which is fundamental to application security. Secret keys/signatures are used while dealing with JWT tokens in web authorisation paradigm which should stay confidential. &lt;br&gt;
These values should not be stored in codebase or program files as they might end up getting checked/committed into version control systems which poses a threat to system integrity.&lt;br&gt;
In a production environment these varibles are set on the system/server running the app instance. We can set up these variables as environment variables in our local development system but it is an unnecessary hassle and can be avoided.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is the way around?
&lt;/h1&gt;

&lt;p&gt;There are various ways to managing environment variables in local development environment. Here, I will display one of the methods.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a .env file and store your secrets in it.&lt;/li&gt;
&lt;li&gt;Include .env in your .gitignore to prevent it from getting checked into your version control system.&lt;/li&gt;
&lt;li&gt;An example .env file might look like this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALGORITHM=HS256
SECRET_KEY=fajhfkaheufahfjbavaeua26472647yhf93h84go84g7fefaefavaegaebartttb
ACCESS_TOKEN_EXPIRE_MINUTES=30
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now we need a way to access these values in our Python program files. Here, we are going to use a Python library called &lt;a href="https://pydantic-docs.helpmanual.io/"&gt;Pydantic&lt;/a&gt; to manage our enviroment variables.&lt;/li&gt;
&lt;li&gt;Go ahead and install "Pydantic" library by executing command &lt;code&gt;pip install pydantic&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Create a file called &lt;code&gt;config.py&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Inside &lt;code&gt;config.py&lt;/code&gt; import BaseSettings from pydantic library and create a class &lt;code&gt;Settings&lt;/code&gt; which inherits from &lt;code&gt;BaseSettings&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Inside this class, type out the environment variable name and their datatype with a : in between. &lt;/li&gt;
&lt;li&gt;Create a class inside class Settings with name &lt;code&gt;Config&lt;/code&gt; and define a variable called &lt;code&gt;env_file = ".env"&lt;/code&gt; which stores the name of your file containing values of secrets we created earlier.
-An example &lt;code&gt;config.py&lt;/code&gt; file would look like this:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from pydantic import BaseSettings

class Settings(BaseSettings):
    secret_key: str
    algorithm: str
    access_token_expire_minutes: int

    class Config:
        env_file = ".env"


settings = Settings()

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;You can also define a default values for these parameters too. Example: &lt;code&gt;access_token_expire_minutes: int = 30&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Instantiate the class Settings as shown in example above.&lt;/li&gt;
&lt;li&gt;Now open the file which contains requires these secret values and import this &lt;code&gt;settings&lt;/code&gt; object in that file by typing &lt;code&gt;from .config import settings&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Now we can assign values of the variables as :
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALGORITHM = settings.algorithm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Similarly do this for all other variables present in your project.&lt;/li&gt;
&lt;li&gt;You are done! Voila!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You have successfully avoided pushing your project secrets to version control system and have setup your local development enviroment without messing your OS installation.&lt;/p&gt;

&lt;p&gt;Do leave a thumbs up if this was helpful. Suggestions are always welcome! Thanks for reading.&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to get started with Pipenv?</title>
      <dc:creator>Raghavendra Khare</dc:creator>
      <pubDate>Sun, 29 Aug 2021 05:26:46 +0000</pubDate>
      <link>https://forem.com/raghavxk/how-to-get-started-with-pipenv-obl</link>
      <guid>https://forem.com/raghavxk/how-to-get-started-with-pipenv-obl</guid>
      <description>&lt;p&gt;*This blog has been republished from Hashnode blog.&lt;br&gt;
In this blog post I will discuss how to get started with &lt;a href="https://pipenv.pypa.io/en/latest/"&gt;Pipenv&lt;/a&gt; - a python packaging tool. The blog post follows Ubuntu workflow which can be easily replicated in MacOS and Windows. Let's get started.&lt;/p&gt;
&lt;h3&gt;
  
  
  What is &lt;a href="https://pipenv.pypa.io/en/latest/"&gt;Pipenv&lt;/a&gt; ?
&lt;/h3&gt;

&lt;p&gt;Pipenv is  a python packaging tool for Python and an upgrade over using &lt;a href="https://pip.pypa.io/en/stable/"&gt;Pip&lt;/a&gt; , &lt;a href="https://docs.python.org/3/library/venv.html"&gt;Venv&lt;/a&gt; and requirements.txt . Pipenv is a great way to combine package management with virtual environments. &lt;/p&gt;
&lt;h3&gt;
  
  
  Why do we need Package Management and Virtual environments?
&lt;/h3&gt;

&lt;p&gt;According to Wlkipedia ,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A package manager or package-management system is a collection of software tools that automates the process of installing, upgrading, configuring, and removing computer programs for a computer's operating system in a consistent manner.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A package manager automates the process of installing, uninstalling, maintaining a package. This helps developers easily manage a project's dependencies.&lt;/p&gt;

&lt;p&gt;You can read more about package managers &lt;a href="https://en.wikipedia.org/wiki/Package_manager#Functions"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now let's discuss virtual environments,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A virtual environment is a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A virtual environment enables us to have a specific unique python installation for each project. This prevents us from overloading the global python installation and enables us to use different versions of python for each project. &lt;br&gt;
A python virtual environment also helps segregate individual dependencies for each project and prevents code from breaking in case if any project was specifically configured to a version of Python.&lt;br&gt;
You can read about virtual environments in detail &lt;a href="https://docs.python.org/3/tutorial/venv.html"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now, that we understand what are package managers and why we need them let's get started with installation of Pipenv.&lt;/p&gt;
&lt;h3&gt;
  
  
  How to install Pipenv?
&lt;/h3&gt;

&lt;p&gt;To install pipenv, open a terminal window and run 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;$ pip install pipenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How to create a virtual environment using PIpenv?
&lt;/h3&gt;

&lt;p&gt;Navigate into the directory you want to create a virtual environment in and open a terminal window and type 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;$ mkdir my_project
$ cd my_project/
$ pipenv install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How to start a virtual environment using PIpenv?
&lt;/h3&gt;

&lt;p&gt;To start a virtual environment, type the following command while being in the directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pipenv shell
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will see a project name within parenthesis indicating we have successfully entered the required python virtual environment.&lt;/p&gt;

&lt;p&gt;To exit the virtual environment we can type,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ exit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How to check the which Python installation is in active use ?
&lt;/h3&gt;

&lt;p&gt;To check which python installation in use we can use the following 3 methods,&lt;/p&gt;

&lt;h4&gt;
  
  
  Method 1:
&lt;/h4&gt;

&lt;p&gt;While the python shell is active type 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;$ which python
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will return the path of current python environment in active use.&lt;/p&gt;

&lt;h4&gt;
  
  
  Method 2:
&lt;/h4&gt;

&lt;p&gt;Type this in an active python shell,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ import sys
$ sys.executable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will return the path of python installation in active use.&lt;/p&gt;

&lt;h4&gt;
  
  
  Method 3:
&lt;/h4&gt;

&lt;p&gt;To find path of executable without activating shell following command can we used :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pipenv --venv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How to install packages using Pipenv?
&lt;/h3&gt;

&lt;p&gt;Type the following code to install a package usign Pipenv.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pipenv install &amp;lt;package-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How to run a Python command without activating virtual environment within the current environment?
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pipenv run python
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To run a file, use 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;$ pipenv run  python &amp;lt;file-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How to use requirements.txt file with Pipenv?
&lt;/h3&gt;

&lt;p&gt;To install dependencies and packages using pip's requirements.txt , use 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;$ pipenv install -r &amp;lt;path-of-requirements.txt&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How to create a requirements.txt using Pipenv?
&lt;/h3&gt;

&lt;p&gt;Following command can be used to generate the content of requirements.txt :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pipenv lock -r 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To create a requirements.txt we can redirect this output to our requirements.txt :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pipenv lock -r &amp;gt; requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How to uninstall a package using Pipenv?
&lt;/h3&gt;

&lt;p&gt;Following command can be used to uninstall a package using pipenv :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pipenv uninstall &amp;lt;package-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To uninstall all packages use &lt;code&gt;-all&lt;/code&gt; flag.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to remove a virtual environment using Pipenv?
&lt;/h3&gt;

&lt;p&gt;Following command can be used to safely remove a package using pipenv :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ pipenv -rm
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Additional points to know about Pipenv :
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;By default, pipenv install virtual environments at &lt;code&gt;~/.local/share/virtualenvs/&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To install a package that shouldn't be included in production build we can use &lt;code&gt;--dev&lt;/code&gt; flag at end of install command.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To check security vulnerabilities in a virtual environment we can use following command :&lt;br&gt;
&lt;code&gt;$ pipenv check&lt;/code&gt; .&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;All dependencies of project can be tracked using following command:&lt;br&gt;
&lt;code&gt;$ pipenv graph&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;This was first blog on pipenv and I will be writing in detail on pipenv in a few more blogs.&lt;/p&gt;

&lt;p&gt;Do leave a thumbs up if this was helpful.&lt;br&gt;
You can follow me on twitter &lt;a href="https://twitter.com/raghavxk"&gt;@raghavxk&lt;/a&gt; to stay posted about my future blogs.&lt;br&gt;
Thanks for reading!&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
