{"id":2515,"date":"2023-02-28T18:24:11","date_gmt":"2023-02-28T18:24:11","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=2515"},"modified":"2023-02-28T19:59:21","modified_gmt":"2023-02-28T19:59:21","slug":"venvansible","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2023\/02\/28\/venvansible\/","title":{"rendered":"Python Virtual Enviroments"},"content":{"rendered":"\n<div class=\"wp-block-media-text alignwide is-stacked-on-mobile\"><figure class=\"wp-block-media-text__media\"><img decoding=\"async\" width=\"139\" height=\"129\" src=\"https:\/\/blog.samarthya.me\/wps\/wp-content\/uploads\/2023\/02\/an-lint-1.png\" alt=\"Using a virtual environment like venv is a best practice when working with Python-based projects like Ansible, Molecule, and Playbooks. Virtual environments are isolated Python environments that allow you to manage dependencies separately for each project, providing a controlled and reproducible environment for development, testing, and production.\" class=\"wp-image-2516 size-full\"\/><\/figure><div class=\"wp-block-media-text__content\">\n<pre class=\"wp-block-preformatted has-medium-font-size\">Using a virtual environment like <code>venv<\/code> is a best practice when working with Python-based projects like <code>Ansible<\/code>, <code>Molecule<\/code>, and <code>Playbooks<\/code>.<\/pre>\n<\/div><\/div>\n\n\n\n<p>Continuing from the <a href=\"https:\/\/blog.samarthya.me\/wps\/2023\/02\/27\/venv-ansible\/\" target=\"_blank\" rel=\"noreferrer noopener\">blog<\/a>, <code>Virtual environments<\/code> are isolated Python environments that allow you to manage dependencies separately for each project, providing a controlled and reproducible environment for development, testing, and production.<\/p>\n\n\n\n<p>Here are some examples of how to use venv for Ansible, Molecule, and Playbook development:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating a virtual environment<\/h3>\n\n\n\n<p>You can create a virtual environment using the <code>venv<\/code> module that comes with Python. To create a new virtual environment, open your terminal and run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-small-font-size\"><code><code>python3 -m venv myenv\n<\/code><\/code><\/pre>\n\n\n\n<p>This will create a new virtual environment named <code>myenv<\/code> in the current directory.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Activating a virtual environment<\/h3>\n\n\n\n<p>Once you have created a virtual environment, you need to activate it before you can use it. To activate the <code>myenv<\/code> environment, run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-small-font-size\"><code><code>source myenv\/bin\/activate\n<\/code><\/code><\/pre>\n\n\n\n<p>This will activate the virtual environment and you will see the name of your environment in the terminal prompt.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Installing dependencies<\/h3>\n\n\n\n<p>To install dependencies in the virtual environment, you can use pip. For example, to install the <code>ansible<\/code> package, run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-small-font-size\"><code><code>pip install ansible\n<\/code><\/code><\/pre>\n\n\n\n<p>This will install the <code>ansible<\/code> package in the <code>myenv<\/code> virtual environment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Freezing packages<\/h3>\n\n\n\n<p>To freeze the list of installed packages in your virtual environment, you can use the <code>pip freeze<\/code> command. This will generate a list of installed packages and their versions that can be used to recreate the same environment on another machine.<\/p>\n\n\n\n<p>To generate a <code>requirements.txt<\/code> file containing the list of installed packages, run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted has-vivid-green-cyan-background-color has-background has-medium-font-size\"><code>pip freeze &gt; requirements.txt\n<\/code><\/pre>\n\n\n\n<p>This will create a <code>requirements.txt<\/code> file in the current directory containing the list of installed packages.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example Contents<\/h2>\n\n\n\n<pre class=\"wp-block-code has-black-color has-luminous-vivid-orange-background-color has-text-color has-background has-small-font-size\"><code>ansible==6.7.0\nansible-compat==3.0.1\nansible-core==2.13.8\narrow==1.2.3\nattrs==22.2.0\nbinaryornot==0.4.4\ncertifi==2022.12.7\ncffi==1.15.1\nchardet==5.1.0\ncharset-normalizer==3.0.1\nclick==8.1.3\nclick-help-colors==0.9.1\ncookiecutter==2.1.1\ncryptography==39.0.1\ndistro==1.8.0\ndocker==6.0.1\nenrich==1.2.7\nidna==3.4\nimportlib-resources==5.12.0\nJinja2==3.1.2\njinja2-time==0.2.0\njsonschema==4.17.3\nmarkdown-it-py==2.2.0\nMarkupSafe==2.1.2\nmdurl==0.1.2\nmolecule==4.0.4\nmolecule-docker==2.1.0\npackaging==23.0\npkgutil_resolve_name==1.3.10\npluggy==1.0.0\npycparser==2.21\nPygments==2.14.0\npyrsistent==0.19.3\npython-dateutil==2.8.2\npython-slugify==8.0.1\nPyYAML==6.0\nrequests==2.28.2\nresolvelib==0.8.1\nrich==13.3.1\nsix==1.16.0\nsubprocess-tee==0.4.1\ntext-unidecode==1.3\ntyping_extensions==4.5.0\nurllib3==1.26.14\nwebsocket-client==1.5.1\nzipp==3.15.0<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Using a <code>requirements.txt<\/code> file<\/h3>\n\n\n\n<p>To install the packages listed in a <code>requirements.txt<\/code> file, run the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code has-white-color has-black-background-color has-text-color has-background has-small-font-size\"><code><code>pip install -r requirements.txt\n<\/code><\/code><\/pre>\n\n\n\n<p>This will install all the packages listed in the <code>requirements.txt<\/code> file in the current virtual environment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p>By isolating your project&#8217;s dependencies, you can ensure that your code works consistently and avoid compatibility issues with different versions of packages. It also makes it easy to manage dependencies and maintain a consistent development environment. Freezing packages and using a <code>requirements.txt<\/code> file are useful techniques for recreating the same environment on different machines or sharing the environment with other developers.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using a virtual environment like venv is a best practice when working with Python-based projects like Ansible, Molecule, and Playbooks. Continuing from the blog, Virtual environments are isolated Python environments that allow you to manage dependencies separately for each project, providing a controlled and reproducible environment for development, testing, and production. Here are some examples [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":2516,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-2515","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technical"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2515","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=2515"}],"version-history":[{"count":2,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2515\/revisions"}],"predecessor-version":[{"id":2518,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/2515\/revisions\/2518"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/2516"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=2515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=2515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=2515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}