<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ipython &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/ipython/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Sun, 24 Apr 2022 16:07:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>ipython &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>Enabling Python VirtualEnv in JupyterLab</title>
		<link>https://myshittycode.com/2022/03/18/enabling-python-virtualenv-in-jupyterlab/</link>
					<comments>https://myshittycode.com/2022/03/18/enabling-python-virtualenv-in-jupyterlab/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Fri, 18 Mar 2022 21:26:42 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Cloud]]></category>
		<category><![CDATA[Google Cloud Platform]]></category>
		<category><![CDATA[ipython]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Vertex AI Workbench]]></category>
		<category><![CDATA[virtualenv]]></category>
		<guid isPermaLink="false">https://myshittycode.com/?p=1380</guid>

					<description><![CDATA[<p>This post illustrates how you can enable Python virtualenv in GCP JupyterLab so that you can organize your .ipynb files to use different virtual environments to keep track of Python package dependencies. PROBLEM You are using GCP JupyterLab. You want to adhere to the Python development best practices by not polluting the global environment with [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2022/03/18/enabling-python-virtualenv-in-jupyterlab/">Enabling Python VirtualEnv in JupyterLab</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>This post illustrates how you can enable Python virtualenv in GCP JupyterLab so that you can organize your <strong>.ipynb</strong> files to use different virtual environments to keep track of Python package dependencies.</p>



<h2 class="wp-block-heading">PROBLEM</h2>



<ul class="wp-block-list"><li>You are using GCP JupyterLab.</li><li>You want to adhere to the Python development best practices by not polluting the global environment with your Python packages so that you can generate a cleaner &#8220;pip freeze&#8221; in the future.</li><li>You want each Notebook file (.ipynb) to have its own environment so that you can run them with different package versions.</li><li>You configured a Python virtual environment, but &#8220;pip install&#8221; from the Notebook file still installs the packages in the global environment.</li><li>You are fed up.</li></ul>



<h2 class="wp-block-heading">SOLUTION</h2>



<h4 class="wp-block-heading">Configuring Virtual Environments</h4>



<p>In the JupyterLab Notebook&#8217;s terminal, create an empty directory to organize all virtual environments.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
mkdir virtualenv
</pre></div>


<p>Ensure ipykernel is installed. This is used to create new IPython kernels.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
python3 -m pip install ipykernel
</pre></div>


<p>For each new virtual environment, run the following commands to perform these steps:</p>



<ul class="wp-block-list"><li>Get into the base virtual environment directory.</li><li>Define a new virtual environment name. Replace <strong>[NEW_ENV_NAME]</strong> with a new name.</li><li>Create new Python virtual environment.<ul><li>The <strong>&#8211;system-site-packages</strong> option ensures you can still use the &#8220;data-sciencey&#8221; packages that come pre-installed with the GCP JupyterLab Notebook within your new virtual environment. </li></ul></li><li>Jump into the newly created virtual environment.</li><li>Create a new IPython kernel.</li><li>Exit from virtual environment.</li></ul>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
cd virtualenv
VENV=&#x5B;NEW_ENV_NAME] # Update this!
python3 -m venv $VENV --system-site-packages
source $VENV/bin/activate
python -m ipykernel install --user --name=$VENV
deactivate
</pre></div>


<h4 class="wp-block-heading">Configuring Notebook File</h4>



<p>Create a new Notebook file (.ipynb).</p>



<p>In <strong>Select Kernel</strong> dialog, select the kernel that you created. In this example, there are 2 new virtual environments (&#8220;smurfs&#8221; and &#8220;thundercats&#8221;).</p>



<div class="wp-block-image size-large"><figure class="aligncenter is-resized"><a href="https://myshittycode.com/wp-content/uploads/2022/03/screen-shot-2022-03-18-at-3.32.19-pm.png?x45560"><img fetchpriority="high" decoding="async" src="https://myshittycode.com/wp-content/uploads/2022/03/screen-shot-2022-03-18-at-3.32.19-pm.png?x45560" alt="Selecting a kernel in JupyterLab" class="wp-image-1397" width="342" height="342" srcset="https://myshittycode.com/wp-content/uploads/2022/03/screen-shot-2022-03-18-at-3.32.19-pm.png 684w, https://myshittycode.com/wp-content/uploads/2022/03/screen-shot-2022-03-18-at-3.32.19-pm-300x300.png 300w, https://myshittycode.com/wp-content/uploads/2022/03/screen-shot-2022-03-18-at-3.32.19-pm-150x150.png 150w" sizes="(max-width: 342px) 100vw, 342px" /></a></figure></div>



<p>To perform a simple test, install a new package.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
%pip install pandas==1.3.0
</pre></div>


<p><strong>IMPORTANT:</strong> You need to use IPython&#8217;s <a href="https://ipython.readthedocs.io/en/stable/interactive/magics.html" target="_blank" rel="noopener">Magics</a> (literally speaking) to ensure the packages are installed in the virtual environment.</p>



<ul class="wp-block-list"><li><strong>%pip</strong> = This uses the pip package manager within the current kernel. Magic!</li><li><strong>! pip</strong> = This uses the pip package manager from the underlying OS. No magic!</li></ul>



<p>From the menu bar, select <strong>Kernel</strong> -&gt; <strong>Restart Kernel and Clear All Outputs</strong>&#8230; . Always restart the kernel when new packages are installed with <strong>%pip</strong>.</p>



<div class="wp-block-image size-large"><figure class="aligncenter is-resized"><a href="https://myshittycode.com/wp-content/uploads/2022/03/screen_shot_2022-03-18_at_3_41_47_pm.png?x45560"><img decoding="async" src="https://myshittycode.com/wp-content/uploads/2022/03/screen_shot_2022-03-18_at_3_41_47_pm.png?x45560" alt="Kernel -&gt; Restart Kernel and Clear All Outputs in JupyterLab" class="wp-image-1398" width="388" height="332" srcset="https://myshittycode.com/wp-content/uploads/2022/03/screen_shot_2022-03-18_at_3_41_47_pm.png 776w, https://myshittycode.com/wp-content/uploads/2022/03/screen_shot_2022-03-18_at_3_41_47_pm-300x257.png 300w, https://myshittycode.com/wp-content/uploads/2022/03/screen_shot_2022-03-18_at_3_41_47_pm-768x657.png 768w" sizes="(max-width: 388px) 100vw, 388px" /></a></figure></div>



<p>Inspect the package version. This should show the version you have just installed.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: bash; title: ; notranslate">
import pandas
print(pandas.__version__)
</pre></div>


<p>To verify this actually works, create another Notebook file pointing to another kernel. In this file, install the same package but with different version.</p>



<figure class="wp-block-image size-large"><a href="https://myshittycode.com/wp-content/uploads/2022/03/screen_shot_2022-03-18_at_3_57_05_pm-2.png?x45560"><img decoding="async" width="2394" height="1586" src="https://myshittycode.com/wp-content/uploads/2022/03/screen_shot_2022-03-18_at_3_57_05_pm-2.png?x45560" alt="Testing Python Virtualenv in JupyterLab" class="wp-image-1394" srcset="https://myshittycode.com/wp-content/uploads/2022/03/screen_shot_2022-03-18_at_3_57_05_pm-2.png 2394w, https://myshittycode.com/wp-content/uploads/2022/03/screen_shot_2022-03-18_at_3_57_05_pm-2-300x199.png 300w, https://myshittycode.com/wp-content/uploads/2022/03/screen_shot_2022-03-18_at_3_57_05_pm-2-1024x678.png 1024w, https://myshittycode.com/wp-content/uploads/2022/03/screen_shot_2022-03-18_at_3_57_05_pm-2-768x509.png 768w, https://myshittycode.com/wp-content/uploads/2022/03/screen_shot_2022-03-18_at_3_57_05_pm-2-1536x1018.png 1536w, https://myshittycode.com/wp-content/uploads/2022/03/screen_shot_2022-03-18_at_3_57_05_pm-2-2048x1357.png 2048w" sizes="(max-width: 2394px) 100vw, 2394px" /></a></figure>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2022/03/18/enabling-python-virtualenv-in-jupyterlab/">Enabling Python VirtualEnv in JupyterLab</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2022/03/18/enabling-python-virtualenv-in-jupyterlab/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1380</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching using Disk

Served from: myshittycode.com @ 2026-02-20 23:55:29 by W3 Total Cache
-->