<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mario Kostelac</title>
    <description>The latest articles on DEV Community by Mario Kostelac (@mariokostelac).</description>
    <link>https://dev.to/mariokostelac</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F376429%2F11762518-a3b7-4443-8a21-7fe1b71d8337.jpg</url>
      <title>DEV Community: Mario Kostelac</title>
      <link>https://dev.to/mariokostelac</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mariokostelac"/>
    <language>en</language>
    <item>
      <title>Github Actions: using python version from .python-version file (pyenv)</title>
      <dc:creator>Mario Kostelac</dc:creator>
      <pubDate>Fri, 09 Apr 2021 08:41:56 +0000</pubDate>
      <link>https://dev.to/mariokostelac/github-actions-using-python-version-from-pythonversion-file-pyenv-5ek2</link>
      <guid>https://dev.to/mariokostelac/github-actions-using-python-version-from-pythonversion-file-pyenv-5ek2</guid>
      <description>&lt;p&gt;I was recently creating a CI pipeline for a toy ML project. It was to make sure that our training accuracies stay within some thresholds as we change the code. We were hosting the project on GitHub so GitHub Actions seemed like a great fit to run our CI.&lt;/p&gt;

&lt;p&gt;I’d found GitHub’s &lt;a href="https://github.com/actions/setup-python"&gt;actions/setup-python@v2&lt;/a&gt; to set up the specific python version, but it wasn’t clear how to set a version used &lt;a href="https://github.com/pyenv/pyenv"&gt;pyenv&lt;/a&gt; (a great tool for managing multiple python versions btw), one written in the &lt;code&gt;.python-version&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Turns out combining &lt;a href="https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions"&gt;action contexts&lt;/a&gt; and &lt;a href="https://docs.github.com/en/actions/reference/environment-variables"&gt;environment variables&lt;/a&gt; does the trick.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;One step reads the &lt;code&gt;.python-version&lt;/code&gt; file and writes that to environment variable&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/actions/setup-python"&gt;actions/setup-python@v2&lt;/a&gt; installs the version from the environment variable.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here's the snippet defining the job steps&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt; &lt;span class="na"&gt;steps&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/checkout@v2&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Get python version&lt;/span&gt;
      &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="pi"&gt;|&lt;/span&gt;
        &lt;span class="s"&gt;python_version=$(cat .python-version)&lt;/span&gt;
        &lt;span class="s"&gt;echo "python_version=${python_version}" &amp;gt;&amp;gt; $GITHUB_ENV&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Set up Python ${{ env.python_version }}&lt;/span&gt;
      &lt;span class="na"&gt;uses&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;actions/setup-python@v2&lt;/span&gt;
      &lt;span class="na"&gt;with&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;python-version&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;${{ env.python_version }}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>ci</category>
      <category>githubactions</category>
    </item>
    <item>
      <title>Importing packages in Jupyter notebooks</title>
      <dc:creator>Mario Kostelac</dc:creator>
      <pubDate>Thu, 07 Feb 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/mariokostelac/importing-packages-in-jupyter-notebooks-2a65</link>
      <guid>https://dev.to/mariokostelac/importing-packages-in-jupyter-notebooks-2a65</guid>
      <description>&lt;p&gt;&lt;em&gt;Seeing “ImportError: No module named tensorflow” but you know you installed it? Sometimes you can import packages from the console, but not from the Jupyter notebook? !pip install tensorflow sometimes just does not work?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It’s not about you. It’s not about python being flaky.It’s actually about your machine &lt;strong&gt;running multiple python installations (environments).&lt;/strong&gt;Let’s build a basic understanding of what’s happening there and what causes your packages missing even after being installed. Once you understand that, you’ll have fewer problems installing and importing packages you need.&lt;/p&gt;

&lt;h2&gt;
  
  
  You are running multiple environments of python. That is fine!
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_HX7G9oV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://biasandvariance.com/images/importing-packages-in-jupyter/thisisfine.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_HX7G9oV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://biasandvariance.com/images/importing-packages-in-jupyter/thisisfine.jpg" alt=""&gt;&lt;/a&gt;Not fine like this! It is actually fine and not your fault. Because not all python2 tools are ported to python3, it is possible that your operating system runs both versions without you touching anything.&lt;/p&gt;

&lt;p&gt;For example, I have both python 2 and python 3 installed. When I call python in my console – python 2.7.10 gets invoked; when I call python3, python 3.7.1 is invoked. It gets even better. I have multiple environments of python 3.6.3 (yes, all the same version) and they load dynamically depending on the project I am currently working on (thanks to conda). I create and destroy python environments daily.&lt;/p&gt;

&lt;p&gt;So how do I know what environment is currently running? Easy! By running &lt;code&gt;import sys; sys.prefix&lt;/code&gt; in interactive console. That reveals what environment of python you are currently in.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--81U5nRJa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://biasandvariance.com/images/importing-packages-in-jupyter/importsys.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--81U5nRJa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://biasandvariance.com/images/importing-packages-in-jupyter/importsys.png" alt="" title="By default, my python loads from ~/anaconda3/bin. When I activate fastai environment, it loads from ~/anaconda3/envs/fastai/bin. These two environments do not share installed packages!&amp;lt;br&amp;gt;
"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Different python environment means different sets of packages
&lt;/h3&gt;

&lt;p&gt;Unless you’ve modified &lt;code&gt;$PYTHONPATH&lt;/code&gt; variable (it’s ok if you haven’t heard of it), each of these environments will use a completely separate set of packages. That is the reason you can’t import the package and you know you installed it. You installed it in the wrong environment!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;But you’re running Jupyter and not the console and how does it all fit together?&lt;/em&gt; We’re getting there just now.&lt;/p&gt;
&lt;h2&gt;
  
  
  Jupyter kernel you use is not using the same python environment as your console.
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Jupyter does not run python the same way your console does&lt;/strong&gt;. It has a concept of a kernel (if you are not familiar with that concept, think about it as python environment registered with Jupyter). The kernel running your notebook likely uses a different python environment and definitely does not have all the environment variables set as your console does. That is fine, too. We know how to figure out which environment is running our code so we can do exactly the same in Jupyter notebook.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--71oDgRFL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://biasandvariance.com/images/importing-packages-in-jupyter/twoenvs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--71oDgRFL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://biasandvariance.com/images/importing-packages-in-jupyter/twoenvs.png" alt="" title="Two different kernels are configured to use two different python environments. Running sys.prefix shows the current environment running the notebook."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Aha!!! You know which environment Jupyter uses. Now you just have to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;make sure your console (temporarily) uses the same python environment as your Jupyter notebook.&lt;/li&gt;
&lt;li&gt;install the package with &lt;code&gt;conda install&lt;/code&gt; or &lt;code&gt;pip install&lt;/code&gt; (if you don’t know what is the difference, quickly go to &lt;a href="https://dev.to/how-not-to-break-your-python-environment-again/"&gt;this guide&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cool, cool, cool. But you don’t know how to make your console use the same environment? All good, we’ll do it from your Jupyter notebook, but not like the last time you did it.&lt;/p&gt;
&lt;h2&gt;
  
  
  You are installing packages straight from Jupyter notebook and you are doing it wrong.
&lt;/h2&gt;

&lt;p&gt;Browsing through StackOverflow about similar issues made me realise people are suggesting the thing that won’t work most of the time. If you are installing packages by running&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;!conda install tensorflow
# or if you want to use pip
!pip install tensorflow

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;you are using very fragile commands (if run in notebook) and that’s the reason packages you installed can’t be imported. It is not fine this time. But we will fix it 🙏. If you are interested in low-level details about why it does not work, read &lt;a href="https://jakevdp.github.io/blog/2017/12/05/installing-python-packages-from-jupyter/"&gt;this great blog post from Jake Vanderplas&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Instead, use these commands:
&lt;/h2&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import sys
!conda install --yes --prefix {sys.prefix} tensorflow
# or if you want to use pip
!{sys.executable} -m pip install tensorflow

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;and you will not have problems with damn ImportError again.&lt;/p&gt;

&lt;p&gt;Understanding these concepts helped me and my teammates to understand what’s happening and why we were getting these errors. If you think I made a mistake, I missed something or you just want to say thanks, please do that in comments 🗣.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>jupyter</category>
      <category>python</category>
    </item>
    <item>
      <title>What is the difference between conda and pip and how not to break your environment again?</title>
      <dc:creator>Mario Kostelac</dc:creator>
      <pubDate>Sat, 02 Feb 2019 00:00:00 +0000</pubDate>
      <link>https://dev.to/mariokostelac/what-is-the-difference-between-conda-and-pip-and-how-not-to-break-your-environment-again-4bhi</link>
      <guid>https://dev.to/mariokostelac/what-is-the-difference-between-conda-and-pip-and-how-not-to-break-your-environment-again-4bhi</guid>
      <description>&lt;p&gt;One of the first things I’ve noticed while coming into Machine Learning Engineer role was package management mess.People often ask me &lt;em&gt;“Should I use conda or pip to install packages?”. “Is conda just python with preinstalled packages?”. “I am getting some compiler errors. I thought we’re using just python?!”.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And I did not know what to answer. Everybody is mix and matching words pip, conda and install until it starts working! And it usually works, until it breaks your environment completely. &lt;strong&gt;And you do not know why it broke so you remove and install all packages from scratch.&lt;/strong&gt; I was there. I can guarantee it does not have to be like that!&lt;/p&gt;

&lt;p&gt;After you read what pip and conda do and how they work, you won’t be breaking your environment again.&lt;/p&gt;

&lt;h2&gt;
  
  
  pip
&lt;/h2&gt;

&lt;p&gt;pip (recursive “pip Installs Packages”) is a Python package installer. It downloads and installs packages you want to use. Conda does that as well. The difference is that pip does just that and nothing else!&lt;/p&gt;

&lt;p&gt;Pip is very simple:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it does not support multiple versions of same package installed - that’s why you can’t have to projects using different versions of same package!&lt;/li&gt;
&lt;li&gt;for many packages, it uses your compiler, which could be incompatible - this is where your compiler errors are coming from&lt;/li&gt;
&lt;li&gt;it does not know anything about python versions - python 2, python 3? Python 3.5 or 3.6? It can’t help with that.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  conda
&lt;/h2&gt;

&lt;p&gt;conda does far more than pip! It’s made for people like you, like me, people doing machine learning and data science.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It allows you to use different versions of the same package.&lt;/li&gt;
&lt;li&gt;It gives you the power to use &lt;strong&gt;any python version&lt;/strong&gt; you want!&lt;/li&gt;
&lt;li&gt;It compiles packages before publishing to the package repository so you don’t get compiler errors.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;(It actually does far more, but we’ll stop here 🤓)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conda&lt;/strong&gt; can do that because &lt;strong&gt;is a package manager, but also an environment manager&lt;/strong&gt;. It isolates different python and package versions so they do not interact with each other.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use one conda per project to stay away from the trouble!
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;If you don’t have it installed, &lt;a href="https://www.anaconda.com/distribution/"&gt;go install anaconda&lt;/a&gt;!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;System that helps me stay away from the trouble is to create one conda environment per project. Whenever you start working on a new project, just run conda env create -n .&lt;/p&gt;

&lt;p&gt;If you have a specific version of python you want to use, run &lt;code&gt;conda env create -n &amp;lt;project_name&amp;gt; python=3.5&lt;/code&gt; (for version 3.5).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hxui5tCt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://biasandvariance.com/images/how-not-to-break-env/environment-creation.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hxui5tCt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://biasandvariance.com/images/how-not-to-break-env/environment-creation.png" alt="" title="Prompt showing new conda enviroment creation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It’s going to install some basic packages and give instructions for activating created environment. This is important to remember. You have to activate the environment explicitly every time you want to use it in new console session. But it’s easy, just type &lt;code&gt;conda activate &amp;lt;project_name&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing packages
&lt;/h3&gt;

&lt;p&gt;Once you activate the conda environment, you can install any package you want - just type &lt;code&gt;conda install &amp;lt;package_name&amp;gt;&lt;/code&gt;. Same as you would do with pip.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--951vCTCU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://biasandvariance.com/images/how-not-to-break-env/package-listing.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--951vCTCU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://biasandvariance.com/images/how-not-to-break-env/package-listing.png" alt="" title="If you are not sure in which conda environment you are right now, you can see it at the line beginning! On a system with anaconda installed, the default environment is “base”."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  But I often have these once-off notebooks that don’t fit into any project! 🔧
&lt;/h3&gt;

&lt;p&gt;No problem, just create a onceoff environment! (hint: &lt;code&gt;conda env create -n onceoff&lt;/code&gt;). It’s possible you will break this one, but you remove it with conda env remove onceoff. It is much faster than uninstalling all pip packages or all of conda 🍻.&lt;/p&gt;

&lt;h3&gt;
  
  
  I’ve forgotten the environment name 🤦🏼‍♂️
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;conda env list&lt;/code&gt; will list all existing environments.&lt;/p&gt;

&lt;p&gt;This setup saved me hours of debugging issues, figuring out it’s the wrong package version and then reinstalling everything from scratch. I’m interested to hear how it works for you! Or if it does not, what does? 🗣&lt;/p&gt;

</description>
      <category>aws</category>
      <category>python</category>
    </item>
  </channel>
</rss>
