<?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: Mahdi Ahmadi</title>
    <description>The latest articles on DEV Community by Mahdi Ahmadi (@mrnik).</description>
    <link>https://dev.to/mrnik</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%2F2872286%2Fe105859e-d279-4cc0-9691-d9295db45988.jpg</url>
      <title>DEV Community: Mahdi Ahmadi</title>
      <link>https://dev.to/mrnik</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mrnik"/>
    <language>en</language>
    <item>
      <title>Complete Guide to Virtual Environments (Virtualenv) in Python</title>
      <dc:creator>Mahdi Ahmadi</dc:creator>
      <pubDate>Mon, 17 Feb 2025 02:02:26 +0000</pubDate>
      <link>https://dev.to/mrnik/complete-guide-to-virtual-environments-virtualenv-in-python-3jn1</link>
      <guid>https://dev.to/mrnik/complete-guide-to-virtual-environments-virtualenv-in-python-3jn1</guid>
      <description>&lt;h2&gt;
  
  
  1. What is a Virtual Environment in Python?
&lt;/h2&gt;

&lt;p&gt;When developing multiple projects with Python, each project may require different verzsions of libraries. This is where Virtual Environment (Virtualenv) comes to the rescue!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A virtual environment is an isolated space for installing libraries and packages for a specific project without affecting your main system.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  2. Why Should You Use Virtualenv?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Avoid version conflicts: If different projects require different versions of the same library, conflicts may arise without a virtual environment.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Project isolation: Each project has its own set of dependencies, ensuring stability.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Portability: You can easily recreate the project environment on another system using a requirements.txt file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Increased security: Installing packages in an isolated environment prevents unintended changes to system files.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Installing and Using Virtualenv
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Installing Virtualenv on Windows, Linux, and macOS&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If Virtualenv is not already installed, you can install it using the following command:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;Check installation:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Creating a Virtual Environment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To create a virtual environment in your project directory, run:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;venv is the name of the folder where the virtual environment will be created. You can use any name you prefer.&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Activating the Virtual Environment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The activation process depends on your operating system:&lt;br&gt;
On Windows (CMD or PowerShell):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;venv\Scripts\activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or for PowerShell:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;venv\Scripts\Activate.ps1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On Linux and macOS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;source venv/bin/activate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once activated, you will see the virtual environment name in the terminal prompt:&lt;br&gt;
&lt;code&gt;(venv) user@computer:~$&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Installing Packages in the Virtual Environment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After activation, you can install project dependencies using:&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 django
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Deactivating the Virtual Environment&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To deactivate the virtual environment, simply run:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Saving and Recreating the Virtual Environment with &lt;code&gt;requirements.txt&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;To save the list of installed packages in the virtual environment, use:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip freeze &amp;gt; requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To recreate the same environment on another system:&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 -r requirements.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  5. Conclusion
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Virtualenv helps you run Python projects in an isolated and conflict-free manner.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can install it with pip install virtualenv.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create and activate a virtual environment with venv.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Use requirements.txt to store and restore dependencies.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks for reading❤️&lt;br&gt;
I hope this guide helps you understand and use virtual environments effectively. If you have any questions or suggestions, feel free to leave a comment!&lt;/p&gt;

</description>
      <category>python</category>
      <category>virtualenv</category>
    </item>
  </channel>
</rss>
