{"id":30902,"date":"2022-08-01T10:33:59","date_gmt":"2022-08-01T10:33:59","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=30902"},"modified":"2022-12-23T05:47:58","modified_gmt":"2022-12-23T05:47:58","slug":"python-tutorials-virtualenv","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/python-tutorials-virtualenv\/","title":{"rendered":"Python Tutorials: virtualenv"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Virtual environment manager<\/h2>\n\n\n\n<p>Each integration has its own set of dependencies that must be added to Python in order to run the tests, or just to try out the collection code. To avoid polluting your Python installation with libraries and packages that would only be used by an Integration, use a \u201cvirtual environment\u201d. A virtual environment is a self contained directory tree that contains an isolated Python installation. When a virtual environment is active, any package you install goes into that directory without affecting the system wide Python installation.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Virtualenv and virtualenvwrapper<\/h2>\n\n\n\n<p>Datadog recommends using Virtualenv to manage Python virtual environments, and virtualenvwrapper to make the process smoother. There\u2019s a comprehensive guide in the Hitchhiker\u2019s Guide to Python describing how to set up these two tools<\/p>\n\n\n\n<p><a href=\"http:\/\/pypi.org\/project\/virtualenv\" target=\"_blank\" rel=\"noopener\">virtualenv<\/a>&nbsp;is a tool to create isolated Python environments. virtualenv creates a folder which contains all the necessary executables to use the packages that a Python project would need.<\/p>\n\n\n\n<p>It can be used standalone, in place of Pipenv.<\/p>\n\n\n\n<p>Install virtualenv via pip:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ pip install virtualenv\n<\/pre>\n\n\n\n<p>Test your installation:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ virtualenv --version\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Usage<a href=\"https:\/\/docs.python-guide.org\/dev\/virtualenvs\/#basic-usage\" target=\"_blank\" rel=\"noopener\"><\/a><\/h2>\n\n\n\n<ol class=\"wp-block-list\"><li>Create a virtual environment for a project:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">$ cd project_folder\n$ virtualenv venv\n<\/pre>\n\n\n\n<p><code>virtualenv&nbsp;venv<\/code>&nbsp;will create a folder in the current directory which will contain the Python executable files, and a copy of the&nbsp;<code>pip<\/code>&nbsp;library which you can use to install other packages. The name of the virtual environment (in this case, it was&nbsp;<code>venv<\/code>) can be anything; omitting the name will place the files in the current directory instead.<\/p>\n\n\n\n<p>Note<\/p>\n\n\n\n<p>\u2018venv\u2019 is the general convention used globally. As it is readily available in ignore files (eg: .gitignore\u2019)<\/p>\n\n\n\n<p>This creates a copy of Python in whichever directory you ran the command in, placing it in a folder named&nbsp;<code>venv<\/code>.<\/p>\n\n\n\n<p>You can also use the Python interpreter of your choice (like&nbsp;<code>python2.7<\/code>).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ virtualenv -p \/usr\/bin\/python2.7 venv\n<\/pre>\n\n\n\n<p>or change the interpreter globally with an env variable in&nbsp;<code>~\/.bashrc<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ export VIRTUALENVWRAPPER_PYTHON=\/usr\/bin\/python2.7\n<\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\"><li>To begin using the virtual environment, it needs to be activated:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">$ source venv\/bin\/activate\n<\/pre>\n\n\n\n<p>The name of the current virtual environment will now appear on the left of the prompt (e.g.&nbsp;<code>(venv)Your-Computer:project_folder&nbsp;UserName$<\/code>) to let you know that it\u2019s active. From now on, any package that you install using pip will be placed in the&nbsp;<code>venv<\/code>&nbsp;folder, isolated from the global Python installation.<\/p>\n\n\n\n<p>For Windows, the same command mentioned in step 1 can be used to create a virtual environment. However, activating the environment requires a slightly different command.<\/p>\n\n\n\n<p>Assuming that you are in your project directory:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">C:\\Users\\SomeUser\\project_folder&gt; venv\\Scripts\\activate\n<\/pre>\n\n\n\n<p>Install packages using the&nbsp;<code>pip<\/code>&nbsp;command:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ pip install requests\n<\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\"><li>If you are done working in the virtual environment for the moment, you can deactivate it:<\/li><\/ol>\n\n\n\n<pre class=\"wp-block-preformatted\">$ deactivate\n<\/pre>\n\n\n\n<p>This puts you back to the system\u2019s default Python interpreter with all its installed libraries.<\/p>\n\n\n\n<p>To delete a virtual environment, just delete its folder. (In this case, it would be&nbsp;<code>rm&nbsp;-rf&nbsp;venv<\/code>.)<\/p>\n\n\n\n<p>After a while, though, you might end up with a lot of virtual environments littered across your system, and it\u2019s possible you\u2019ll forget their names or where they were placed.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installation<\/h2>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">python -m pip install --user virtualenv\r\npython -m virtualenv --help<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Linux<a href=\"https:\/\/virtualenv.pypa.io\/en\/latest\/installation.html#linux\" target=\"_blank\" rel=\"noopener\">\u00b6<\/a><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>installations from&nbsp;<a href=\"https:\/\/www.python.org\/downloads\/\" target=\"_blank\" rel=\"noopener\">python.org<\/a><\/li><li>Ubuntu 16.04+ (both upstream and&nbsp;<a href=\"https:\/\/launchpad.net\/~deadsnakes\/+archive\/ubuntu\/ppa\" target=\"_blank\" rel=\"noopener\">deadsnakes<\/a>&nbsp;builds)<\/li><li>Fedora<\/li><li>RHEL and CentOS<\/li><li>OpenSuse<\/li><li>Arch Linux<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">macOS<a href=\"https:\/\/virtualenv.pypa.io\/en\/latest\/installation.html#macos\" target=\"_blank\" rel=\"noopener\">\u00b6<\/a><\/h3>\n\n\n\n<p>In case of macOS we support:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>installations from&nbsp;<a href=\"https:\/\/www.python.org\/downloads\/\" target=\"_blank\" rel=\"noopener\">python.org<\/a><\/li><li>python versions installed via&nbsp;<a href=\"https:\/\/docs.brew.sh\/Homebrew-and-Python\" target=\"_blank\" rel=\"noopener\">brew<\/a>&nbsp;(both older python2.7 and python3)<\/li><li>Python 3 part of XCode (Python framework &#8211;&nbsp;<code>\/Library\/Frameworks\/Python3.framework\/<\/code>)<\/li><li>Python 2 part of the OS (<code>\/System\/Library\/Frameworks\/Python.framework\/Versions\/<\/code>)<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Windows<a href=\"https:\/\/virtualenv.pypa.io\/en\/latest\/installation.html#windows\" target=\"_blank\" rel=\"noopener\">\u00b6<\/a><\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Installations from&nbsp;<a href=\"https:\/\/www.python.org\/downloads\/\" target=\"_blank\" rel=\"noopener\">python.org<\/a><\/li><li>Windows Store Python &#8211; note only&nbsp;<a href=\"https:\/\/www.microsoft.com\/en-us\/p\/python-38\/9mssztt1n39l\" target=\"_blank\" rel=\"noopener\">version 3.7+<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Virtual environment manager Each integration has its own set of dependencies that must be added to Python in order to run the tests, or just to try out the collection code. To avoid polluting your Python installation with libraries and packages that would only be used by an Integration, use a \u201cvirtual environment\u201d. A virtual&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[2],"tags":[],"class_list":["post-30902","post","type-post","status-publish","format-standard","hentry","category-uncategorised"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30902","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=30902"}],"version-history":[{"count":4,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30902\/revisions"}],"predecessor-version":[{"id":30907,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/30902\/revisions\/30907"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=30902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=30902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=30902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}