<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>venv | Cameron McLean</title><link>https://www.cammcl.com/tag/venv/</link><atom:link href="https://www.cammcl.com/tag/venv/index.xml" rel="self" type="application/rss+xml"/><description>venv</description><generator>Wowchemy (https://wowchemy.com)</generator><language>en-us</language><copyright>Copyright © Cameron McLean, 2021</copyright><lastBuildDate>Fri, 01 Jan 2021 00:00:00 +0000</lastBuildDate><image><url>https://www.cammcl.com/images/icon_hu3691f1e33ccd83293b0016f31ce1d440_183416_512x512_fill_lanczos_center_2.png</url><title>venv</title><link>https://www.cammcl.com/tag/venv/</link></image><item><title>Virtual Environments with venv</title><link>https://www.cammcl.com/notes/python/virtual_environments/</link><pubDate>Fri, 01 Jan 2021 00:00:00 +0000</pubDate><guid>https://www.cammcl.com/notes/python/virtual_environments/</guid><description>&lt;h2>Table of Contents&lt;/h2>
&lt;nav id="TableOfContents">
&lt;ul>
&lt;li>
&lt;ul>
&lt;li>&lt;a href="#what-is-a-virtual-environment">What is a Virtual Environment&lt;/a>&lt;/li>
&lt;li>&lt;a href="#creating-a-virtual-environment-with-venv">Creating a Virtual Environment with venv&lt;/a>&lt;/li>
&lt;li>&lt;a href="#activating-a-virtual-environment">Activating a Virtual Environment&lt;/a>&lt;/li>
&lt;li>&lt;a href="#deactivating-a-virtual-environment">Deactivating a Virtual Environment&lt;/a>&lt;/li>
&lt;li>&lt;a href="#add-the-virtual-environment-as-a-python-kernel-in-jupyter">Add the Virtual Environment as a Python Kernel in Jupyter&lt;/a>&lt;/li>
&lt;li>&lt;a href="#list-kernels-added-in-jupyter">List Kernels added in Jupyter&lt;/a>&lt;/li>
&lt;li>&lt;a href="#remove-a-kernel">Remove a Kernel&lt;/a>&lt;/li>
&lt;li>&lt;a href="#references">References&lt;/a>&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;/nav>
&lt;h2 id="what-is-a-virtual-environment">What is a Virtual Environment&lt;/h2>
&lt;p>A virtual environment is an isolated environment where the Python interpreter, libraries and scripts installed are kept completely separate from other environments including the system environment installed on your operating system.&lt;/p>
&lt;h2 id="creating-a-virtual-environment-with-venv">Creating a Virtual Environment with venv&lt;/h2>
&lt;p>Since Python 3.3. Python has included venv in its standard libraries. To create a virtual environment with the name name_of_venv type &lt;code>python -m venv name_of_venv&lt;/code>&lt;/p>
&lt;h2 id="activating-a-virtual-environment">Activating a Virtual Environment&lt;/h2>
&lt;p>To activate the virtual environment type &lt;code>name_of_venv\Scripts\activate python&lt;/code>&lt;/p>
&lt;p>The command line interface will now start with (name_of_venv) to show you it has been activated.&lt;/p>
&lt;p>You can now install packages with pip: &lt;code>pip install jupyter&lt;/code>&lt;/p>
&lt;h2 id="deactivating-a-virtual-environment">Deactivating a Virtual Environment&lt;/h2>
&lt;p>To deactivate the virtual environment type &lt;code>deactivate&lt;/code>&lt;/p>
&lt;h2 id="add-the-virtual-environment-as-a-python-kernel-in-jupyter">Add the Virtual Environment as a Python Kernel in Jupyter&lt;/h2>
&lt;p>Jupyter uses the default system IPython kernel but you have to manually add a kernel for the virtual environment you just created.&lt;/p>
&lt;p>To add the virtual enviroment to Jupyter, first you need to:&lt;/p>
&lt;ol>
&lt;li>activate the environment&lt;/li>
&lt;li>install jupyter (which includes the library ipykernel)&lt;/li>
&lt;li>type &lt;code>python -m ipykernel install --user --name=name_of_venv&lt;/code>&lt;/li>
&lt;/ol>
&lt;h2 id="list-kernels-added-in-jupyter">List Kernels added in Jupyter&lt;/h2>
&lt;p>type &lt;code>jupyter kernelspec list&lt;/code>&lt;/p>
&lt;h2 id="remove-a-kernel">Remove a Kernel&lt;/h2>
&lt;p>type &lt;code>jupyter kernelspec remove name_of_venv&lt;/code>&lt;/p>
&lt;h2 id="references">References&lt;/h2>
&lt;ul>
&lt;li>&lt;a href="https://docs.python.org/3/library/venv.html" target="_blank" rel="noopener">venv Docs&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://ipython.readthedocs.io/en/stable/install/kernel_install.html" target="_blank" rel="noopener">IPython Docs&lt;/a>&lt;/li>
&lt;/ul>
&lt;pre>&lt;code class="language-python">#Versions used in this notebook
import sys
print(&amp;quot;OS:&amp;quot;, sys.platform)
!python --version
from importlib.metadata import version
for library in [&amp;quot;jupyterlab&amp;quot;]:
print(library, version(library))
&lt;/code>&lt;/pre>
&lt;pre>&lt;code>OS: win32
Python 3.9.1
jupyterlab 2.2.0
&lt;/code>&lt;/pre></description></item></channel></rss>