<?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"><channel><title><![CDATA[Paulo Rodrigues]]></title><description><![CDATA[Paulo Rodrigues]]></description><link>https://paulorod7.com</link><generator>RSS for Node</generator><lastBuildDate>Thu, 23 Apr 2026 00:39:51 GMT</lastBuildDate><atom:link href="https://paulorod7.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Best practices to use and manage Python virtual environments]]></title><description><![CDATA[Virtual environments are a useful tool for managing the packages and libraries that you use for different projects.
Here are the main points to follow when using virtual environments:

Create a new virtual environment for each project, using a tool l...]]></description><link>https://paulorod7.com/best-practices-to-use-and-manage-python-virtual-environments</link><guid isPermaLink="true">https://paulorod7.com/best-practices-to-use-and-manage-python-virtual-environments</guid><category><![CDATA[Python]]></category><category><![CDATA[#codenewbies]]></category><category><![CDATA[coding]]></category><category><![CDATA[best practices]]></category><category><![CDATA[helper]]></category><dc:creator><![CDATA[Paulo Rodrigues]]></dc:creator><pubDate>Fri, 06 Jan 2023 20:43:30 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1673037727308/80bf9a23-6c96-469a-9c7f-ec762810ccd3.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Virtual environments are a useful tool for managing the packages and libraries that you use for different projects.</p>
<p>Here are the main points to follow when using virtual environments:</p>
<ul>
<li><p>Create a new virtual environment for each project, using a tool like <code>virtualenv</code> or <code>venv</code>. This will allow you to have a separate environment for each project, with its own installed packages and libraries.</p>
</li>
<li><p>Use the <code>-p</code> flag to specify the version of Python that you want to use for your virtual environment. This will ensure that you are using the correct version of Python for your project, and that any packages you install are compatible with that version.</p>
</li>
<li><p>Use a requirements file to specify the packages that your project depends on. This makes it easy to install all the necessary packages into a new virtual environment, and ensures that all members of your team are working with the same package versions.</p>
</li>
</ul>
<p>By following these best practices, you can effectively use virtual environments to manage the packages and libraries for your Python projects.</p>
<h2 id="heading-creating-a-virtual-environment-using-venv">Creating a virtual environment using venv</h2>
<p>To create a new virtual environment using <code>venv</code>, you can use the following steps:</p>
<ol>
<li><p>Open a terminal or command prompt.</p>
</li>
<li><p>Choose a directory where you want to create your virtual environment.</p>
</li>
<li><p>Run the following command to create a new virtual environment called <code>myenv</code>:</p>
</li>
</ol>
<pre><code class="lang-bash">python -m venv myenv
</code></pre>
<p>This will create a new directory called <code>myenv</code> that contains the files and directories needed for the virtual environment.</p>
<p>To activate the virtual environment, use the following command:</p>
<pre><code class="lang-bash"><span class="hljs-built_in">source</span> myenv/bin/activate
</code></pre>
<p>You should now see the name of your virtual environment in the terminal prompt, indicating that it is active. For example:</p>
<pre><code class="lang-bash">(myenv) $
</code></pre>
<p>To deactivate the virtual environment, use the following command:</p>
<pre><code class="lang-bash">deactivate
</code></pre>
<p>You can now use the virtual environment to install packages and manage your project dependencies.</p>
<h2 id="heading-setting-the-python-version-to-use-in-your-virtual-environment">Setting the Python version to use in your virtual environment</h2>
<p>To specify the version of Python that you want to use for your virtual environment, you can use the <code>-p</code> flag when creating the environment with <code>venv</code>. For example, to create a virtual environment called <code>myenv</code> that uses Python 3.8, you can use the following command:</p>
<pre><code class="lang-bash">python3.8 -m venv myenv
</code></pre>
<p>You can also specify the full path to the Python executable if it is not in your <code>PATH</code>. For example:</p>
<pre><code class="lang-bash">/usr/<span class="hljs-built_in">local</span>/bin/python3.8 -m venv myenv
</code></pre>
<p>Keep in mind that you need to have the specific version of Python installed on your system to create a virtual environment using that version.</p>
<p>Once the virtual environment is created, you can use the <code>python</code> executable inside the virtual environment to run Python scripts and manage packages. This will ensure that you are using the correct version of Python for your project.</p>
<h2 id="heading-using-a-requirements-file-to-specify-the-packages-your-project-needs">Using a requirements file to specify the packages your project needs</h2>
<p>A requirements file is a text file that specifies the packages that your Python project depends on. It is a useful way to manage the dependencies for your project, and makes it easy to install all the necessary packages into a new virtual environment.</p>
<p>To create a requirements file for your project, you can use the <code>pip freeze</code> command. This command generates a list of all the packages and their versions that are currently installed in your virtual environment.</p>
<p>For example, suppose you have a virtual environment with the following packages installed:</p>
<pre><code class="lang-bash">Flask==1.1.2
requests==2.24.0
</code></pre>
<p>To create a requirements file, open a terminal or command prompt and activate your virtual environment. Then run the following command:</p>
<pre><code class="lang-bash">pip freeze &gt; requirements.txt
</code></pre>
<p>This will create a file called <code>requirements.txt</code> in your project directory, with the following contents:</p>
<pre><code class="lang-bash">Flask==1.1.2
requests==2.24.0
</code></pre>
<p>The requirements file specifies the packages and their exact versions that your project depends on. You can then use this file to install all the necessary packages into a new virtual environment by running the following command:</p>
<pre><code class="lang-bash">pip install -r requirements.txt
</code></pre>
<p>This will install all the packages listed in the requirements file into the new virtual environment.</p>
<p>By using a requirements file, you can ensure that all members of your team are working with the same package versions and that you can easily set up a new development environment for your project.</p>
<p>Hope it helps you create virtual environments and manage the package that your project needs. :)</p>
<p>Ref: <a target="_blank" href="https://docs.python.org/3.9/library/venv.html">https://docs.python.org/3.9/library/venv.html</a><br />Ref: <a target="_blank" href="https://pip.pypa.io/en/stable/cli/pip_freeze/">https://pip.pypa.io/en/stable/cli/pip_freeze/</a></p>
]]></content:encoded></item><item><title><![CDATA[Running a Python script in Terminal without losing it by a connection drop]]></title><description><![CDATA[To run a Python script in the background without losing its execution if the connection to the server drops, you can use a tool called nohup to run the script. nohup stands for "no hangup," and it allows you to run a command in the background even if...]]></description><link>https://paulorod7.com/running-a-python-script-in-terminal-without-losing-it-by-a-connection-drop</link><guid isPermaLink="true">https://paulorod7.com/running-a-python-script-in-terminal-without-losing-it-by-a-connection-drop</guid><category><![CDATA[Python]]></category><category><![CDATA[coding]]></category><category><![CDATA[terminal]]></category><dc:creator><![CDATA[Paulo Rodrigues]]></dc:creator><pubDate>Thu, 05 Jan 2023 20:44:03 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/f5pTwLHCsAg/upload/9c805504b14b4c0e77d18e0a73dee90a.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>To run a Python script in the background without losing its execution if the connection to the server drops, you can use a tool called <code>nohup</code> to run the script. <code>nohup</code> stands for "no hangup," and it allows you to run a command in the background even if you close the terminal or log out of the system.</p>
<p>Here's the general syntax for running a Python script in the background with <code>nohup</code>:</p>
<pre><code class="lang-bash">nohup python script.py &amp;
</code></pre>
<p>This will run the script in the background, and the output will be saved to a file called <code>nohup.out</code>.</p>
<p>If you want to run the script in the background and redirect the output to a specific file, you can use the following syntax:</p>
<pre><code class="lang-bash">nohup python script.py &gt; output.log 2&gt;&amp;1 &amp;
</code></pre>
<p>This will run the script in the background and redirect both the standard output and standard error to the file <code>output.log</code>.</p>
<p>You can also use the <code>screen</code> command to run the script in the background. The <code>screen</code> command allows you to create a new terminal session and run the script within that session. You can then detach the session and log out, and the script will continue running in the background.</p>
<p>Here's the general syntax for running a Python script in the background with <code>screen</code>:</p>
<pre><code class="lang-bash">screen -S session_name
python script.py
Ctrl-A D
</code></pre>
<p>This will create a new screen session called <code>session_name</code>, run the script within that session, and then detach the session. The script will continue running in the background.</p>
<p>To reattach to the session later, use the following command:</p>
<pre><code class="lang-bash">screen -r session_name
</code></pre>
<p>This will allow you to view the output of the script and any errors that may have occurred.</p>
<p>I hope it helps if you are getting into a similar situation :)</p>
]]></content:encoded></item></channel></rss>