<?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: Python Course</title>
    <description>The latest articles on DEV Community by Python Course (@py3course).</description>
    <link>https://dev.to/py3course</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%2F405535%2Fc43adbe7-c544-48f5-b35e-46a3ec157b5a.jpg</url>
      <title>DEV Community: Python Course</title>
      <link>https://dev.to/py3course</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/py3course"/>
    <language>en</language>
    <item>
      <title>Setting up a Python environment in 2020</title>
      <dc:creator>Python Course</dc:creator>
      <pubDate>Wed, 10 Jun 2020 05:53:35 +0000</pubDate>
      <link>https://dev.to/py3course/setting-up-a-python-environment-in-2020-3e9e</link>
      <guid>https://dev.to/py3course/setting-up-a-python-environment-in-2020-3e9e</guid>
      <description>&lt;p&gt;It's 2020, and you want to write some Python. Some operating systems, such as MacOS and Ubuntu, come with a pre-installed version. However, sometimes &lt;a href="https://xkcd.com/1987/" rel="noopener noreferrer"&gt;it gets messy&lt;/a&gt;. In this post, we'll discuss how to &lt;em&gt;get it right&lt;/em&gt;™.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
What's the winning recipe?

&lt;ul&gt;
&lt;li&gt;Manage Python versions with PyEnv&lt;/li&gt;
&lt;li&gt;Useful commands&lt;/li&gt;
&lt;li&gt;How can I know what versions are available in PyEnv?&lt;/li&gt;
&lt;li&gt;Managing projects with Pipenv&lt;/li&gt;
&lt;li&gt;Using VS Code as a text editor&lt;/li&gt;
&lt;li&gt;VS Code and Pipenv&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

Installation

&lt;ul&gt;
&lt;li&gt;MacOS&lt;/li&gt;
&lt;li&gt;Ubuntu&lt;/li&gt;
&lt;li&gt;Windows&lt;/li&gt;
&lt;li&gt;Setting up VS Code&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Creating your first project&lt;/li&gt;

&lt;li&gt;Do you want to know more?&lt;/li&gt;

&lt;/ul&gt;

&lt;h1&gt;
  
  
  What's the winning recipe?
&lt;/h1&gt;

&lt;p&gt;After more than ten years of writing Python, I settled on three main tools to support me in my daily endeavors: &lt;a href="https://github.com/pyenv/pyenv" rel="noopener noreferrer"&gt;PyEnv&lt;/a&gt;, &lt;a href="https://github.com/pypa/pipenv" rel="noopener noreferrer"&gt;pipenv&lt;/a&gt; and &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;VS Code&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Manage Python versions with PyEnv
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/pyenv/pyenv" rel="noopener noreferrer"&gt;PyEnv&lt;/a&gt; allows you to have several Python versions installed and easily switch between them. The features' documentation is available on their &lt;a href="https://github.com/pyenv/pyenv" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;. Below, I leave the commands I mostly use.&lt;/p&gt;

&lt;h3&gt;
  
  
  Useful commands
&lt;/h3&gt;

&lt;p&gt;We can list all the available Python versions with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pyenv versions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We'll see the following output in our terminal:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  system
  3.6.10
* 3.8-dev (set by /Users/joaoqalves/.pyenv/version)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;To change the global Python version we need to run:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pyenv global 3.8.2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;If we are working in a &lt;code&gt;~/projects/playground&lt;/code&gt; project, we can also set the version for this particular folder using:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pyenv local 3.6.3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The above command will create a &lt;code&gt;.python-version&lt;/code&gt; file in the directory, as mentioned earlier. You can check-in this file into your &lt;a href="https://git-scm.com/" rel="noopener noreferrer"&gt;git&lt;/a&gt; to have it across different environments.&lt;/p&gt;
&lt;h3&gt;
  
  
  How can I know what versions are available in PyEnv?
&lt;/h3&gt;

&lt;p&gt;Run the following command:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pyenv install --list
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Managing projects with Pipenv
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/pypa/pipenv" rel="noopener noreferrer"&gt;Pipenv&lt;/a&gt; automatically creates and manages &lt;a href="https://docs.python.org/3/tutorial/venv.html" rel="noopener noreferrer"&gt;virtual environments&lt;/a&gt; for your projects. It uses a &lt;code&gt;Pipfile&lt;/code&gt; to declare all the necessary dependencies and a &lt;code&gt;Pipfile.lock&lt;/code&gt; to improve &lt;em&gt;run&lt;/em&gt; reproducibility.&lt;/p&gt;
&lt;h2&gt;
  
  
  Using VS Code as a text editor
&lt;/h2&gt;

&lt;p&gt;There's not much to say here. &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;VS Code&lt;/a&gt; is becoming the &lt;em&gt;de facto&lt;/em&gt; standard text editor. It works well with &lt;a href="https://github.com/pyenv/pyenv" rel="noopener noreferrer"&gt;PyEnv&lt;/a&gt; and &lt;a href="https://github.com/pypa/pipenv" rel="noopener noreferrer"&gt;Pipenv&lt;/a&gt;. &lt;/p&gt;
&lt;h2&gt;
  
  
  VS Code and Pipenv
&lt;/h2&gt;

&lt;p&gt;When opening a project that uses &lt;a href="https://github.com/pypa/pipenv" rel="noopener noreferrer"&gt;Pipenv&lt;/a&gt;, we should configure the Python interpreter used by &lt;a href="https://code.visualstudio.com/" rel="noopener noreferrer"&gt;VS Code&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/Uxg6xR6Qb_o"&gt;
&lt;/iframe&gt;
&lt;/p&gt;
&lt;h1&gt;
  
  
  Installation
&lt;/h1&gt;

&lt;p&gt;This section will show how to install &lt;a href="https://github.com/pyenv/pyenv" rel="noopener noreferrer"&gt;PyEnv&lt;/a&gt;, different Python versions, and &lt;a href="https://github.com/pypa/pipenv" rel="noopener noreferrer"&gt;Pipenv&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; For every Python version, you'll need to set-up &lt;code&gt;pipenv&lt;/code&gt; accordingly.&lt;/p&gt;
&lt;h2&gt;
  
  
  MacOS
&lt;/h2&gt;

&lt;p&gt;On MacOS, the easiest way to install &lt;a href="https://github.com/pyenv/pyenv" rel="noopener noreferrer"&gt;PyEnv&lt;/a&gt; is through &lt;a href="https://brew.sh/" rel="noopener noreferrer"&gt;Homebrew&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew install pyenv &amp;amp;&amp;amp; pyenv install 3.8.2
pyenv global 3.8.2
pip install pipenv
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Ubuntu
&lt;/h2&gt;

&lt;p&gt;First of all, we'll need to install the dependencies:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;apt-get &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; make build-essential libssl-dev zlib1g-dev libbz2-dev &lt;span class="se"&gt;\&lt;/span&gt;
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev &lt;span class="se"&gt;\&lt;/span&gt;
xz-utils tk-dev libffi-dev liblzma-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Then we'll need to run the installer script, available on &lt;a href="https://github.com/pyenv/pyenv-installer" rel="noopener noreferrer"&gt;PyEnv installer&lt;/a&gt; page:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl https://pyenv.run | bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Windows
&lt;/h2&gt;

&lt;p&gt;Unfortunately, &lt;a href="https://github.com/pyenv/pyenv" rel="noopener noreferrer"&gt;PyEnv&lt;/a&gt; does not support Windows out-of-the-box. You may consider using &lt;a href="https://github.com/pyenv-win/pyenv-win" rel="noopener noreferrer"&gt;pyenv-win&lt;/a&gt; fork. Other options are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;a href="https://docs.microsoft.com/en-us/windows/wsl/install-win10" rel="noopener noreferrer"&gt;Windows Linux Subsystem for Windows 10&lt;/a&gt; and to follow the same steps as the Ubuntu installation; or&lt;/li&gt;
&lt;li&gt;Use a Ubuntu virtual-machine for development and follow the above steps&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Setting up VS Code
&lt;/h2&gt;

&lt;p&gt;The first thing we need is to install &lt;a href="https://marketplace.visualstudio.com/items?itemName=ms-python.python" rel="noopener noreferrer"&gt;Python extension for VS Code&lt;/a&gt;. Then we'll be able to run Python files:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpyffxwhakwwgrdndlos0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fpyffxwhakwwgrdndlos0.png" alt="Run on VS Code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are inside a project and running inside a &lt;a href="https://dev.tovirtual-env"&gt;virtual environment&lt;/a&gt;.&lt;/p&gt;
&lt;h1&gt;
  
  
  Creating your first project
&lt;/h1&gt;

&lt;p&gt;Now that we have all the tools set up, we can start creating our first project. We'll create it in a new directory and tell &lt;code&gt;pipenv&lt;/code&gt; that we want a new project using Python 3.8.2 that will make an HTTP request to &lt;a href="https://jsonplaceholder.typicode.com/" rel="noopener noreferrer"&gt;JSON Placeholder's API&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# New directory&lt;/span&gt;
&lt;span class="nb"&gt;mkdir &lt;/span&gt;pyplayground 
&lt;span class="nb"&gt;cd &lt;/span&gt;pyplayground
&lt;span class="c"&gt;# Change the Python version we want to use&lt;/span&gt;
pyenv &lt;span class="nb"&gt;local &lt;/span&gt;3.8.2 
&lt;span class="c"&gt;# Create a new virtual environment for our dependencies&lt;/span&gt;
pipenv shell
&lt;span class="c"&gt;# Install requests in the current virtual environment&lt;/span&gt;
pipenv &lt;span class="nb"&gt;install &lt;/span&gt;requests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Then we'll create a file named &lt;code&gt;placeholder.py&lt;/code&gt; with the following code:&lt;/p&gt;


&lt;div class="ltag__replit"&gt;
  &lt;iframe height="550px" src="https://repl.it/@joaoqalves/ForsakenGrowingDonateware?lite=true"&gt;&lt;/iframe&gt;
&lt;/div&gt;



&lt;p&gt;To run it on your computer, you can do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python placeholder.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To run it on VS Code, click on the run button on the top-right corner, as mentioned above.&lt;/p&gt;

&lt;h1&gt;
  
  
  Do you want to know more?
&lt;/h1&gt;

&lt;p&gt;I'm creating a step-by-step video course for Python. You can subscribe &lt;a href="https://pythoncourse.dev/" rel="noopener noreferrer"&gt;here&lt;/a&gt;!&lt;/p&gt;

</description>
      <category>python</category>
      <category>tutorial</category>
      <category>vscode</category>
    </item>
  </channel>
</rss>
