Learn Python Series (#26) - pipenv & Visual Studio Code

in #utopian-io6 years ago (edited)

Learn Python Series (#26) - pipenv & Visual Studio Code

python_logo.png

Repository

https://github.com/python/cpython

Other relevant repositories

What will I learn?

  • In this episode of the Learn Python Series we will get our feet wet regarding the installation and set-up of pipenv, "the new Python cool" for creating and managing Python virtualenvs (virtual environments),
  • you'll learn about how to run some basic pipenv commands,
  • we'll discuss how to use pipenv in conjunction with your favorite open source code editor - I'll be configuring Visual Studio Code on macOS -, so that you're able to - for example - use code auto-completion together with your pipenv virtualenv!

Requirements

  • A working modern computer running macOS, Windows or Ubuntu;
  • An installed Python 3(.6) distribution, such as (for example) the Anaconda Distribution;
  • The ambition to learn Python programming.

Difficulty

  • Beginner

Curriculum (of the Learn Python Series):

GitHub Account

https://github.com/realScipio

Learn Python Series (#26) - pipenv & Visual Studio Code

Thus far, in the Learn Python Series, we've been using the Anaconda distribution (which is fine), together with iPython / Jupyter Notebooks (which is also fine) as our coding environment. The topics that were covered were however all "small projects" and/or only dealt with exploring the specifics of that very topic.

In order to develop larger, more "real world" projects, we need to start utilizing another set-up. We need a code editor that's able to assist us with developing code in multiple files, and we (preferably) also need a virtual environment where we can configure module dependencies used for that project (program) only: it could very well be that one program requires very specific module versions that might conflict with the specifics of another program running on the very same machine. That's where virtual environments come in, because they allow you to do just that and keep things nice and clean.

pipenv

pipenv is a (relatively new) Python tool that allows you to manage multiple virtual environments for each of your Python programs. It also conveniently automates things for you, such as (un)installing packages, and pipenv combines pip and virtualenv to work together.

Nota bene: for this tutorial, I'll be working on macOS. Installing pipenv on macOS is as simple as brew install pipenv. When working with pip already, on macOS or another operating system, then pip install pipenv will also do the trick.

Visual Studio Code

Of course you can use any code editor that suits you, but for this tutorial I'll be configuring Visual Studio Code (VSC) to work together with pipenv. In case you haven't installed VSC already, just visit https://github.com/Microsoft/vscode and follow the VSC installation instructions found there.

Setting up pipenv & Visual Studio Code to work together

Let's now go over how to set-up pipenv & Visual Studio Code to work together.

(1) Create a Python3 project folder:
Let's assume you've already defined a base folder where your (development) Python projects live on your system, cd into that, and let's create a new project folder called test, like so:
mkdir test

(2) cd into the newly created Python3 project folder:
cd test

(3) Create pipenv Python 3.6 virtual environment and Pipfile:
We can now let pipenv create a new virtual environment using the Python version that we prefer to use for this specific Python project, which will also be automatically used in the future when installing on, for example, another machine. To make that work flawlessly, a Pipfile is created instead of a requirements.txt file (which could be exported from it). Pipfiles hold dependency info about your project packages.
pipenv --python 3.6

(4) pipenv install, pipenv uninstall & pipenv lock
For installing packages and for Pipfile updating, run pipenv install [package files] (and of course pipenv uninstall [package files] for uninstalling them). If you want to ensure repeatable builds and declare all (sub) dependencies, use pipenv lock (which creates a Pipfile.lock file).

At this point, your new project's pipenv virtual environment is "good to go", but you still want to be able to work with it "as normal" of course, so let's configure VSC to work with it following the next steps.

(5) pipenv virtual environment path:
In order to get the absolute pipenv virtual environment path from the bash shell, just run: pipenv --py (and either write it down or put it into to your copy buffer).

(6) open your pipenv project in VSC:
In Visual Studio Code, open your project folder via File > Open

(7) Choose a temporary Python interpreter:
In Visual Studio Code, first open the Command Pallette with CMD-Shift-P or View > Command Pallette.
Then in the Command Pallette, choose: Python: select interpreter ... and pick any Python interpreter, which makes Visual Studio Code create a hidden folder inside the Python project folder, called .vscode/ and inside it a file called settings.json.

(8) Configuring python.pythonPath
Now open settings.json (containing the VSC "Workspace Settings") and copy-paste the pipenv virtual environment path (ending with ../bin/python) read from the command pipenv --py (step 5) as JSON value for the key python.pythonPath. Then save settings.json, restart Visual Studio Code, for the path changes to take effect and re-open the Python Project folder with VSC.

Screen Shot 2018-07-01 at 14.59.53.png

You can now benefit from IntelliSense & code completion while typing! (Completions can also be manually triggered using ⌃ + Space.)

(9) Running your code
VSC comes with a built-in / integrated terminal which you can open via View > Integrated Terminal or Ctrl-backtick.
Again cd into your pipenv project folder and run pipenv shell in order to spawn a pipenv virtual environment shell.
Say you have a file called main.py, then you can run that (as you would without a pipenv environment) via python main.py.

Leaving the pipenv shell is simply done via exit.

Alternatively, you can invoke pipenv shell commands witout activating it explicitly, by using the pipenv run keyword:
pipenv run python main.py

See you in the next episode! Thank you for your time!

Sort:  

Good to see you back with the python tutorials :)

What would I do without my beloved Python? ;-)

Thank you, scipio. Upvoted and resteemed!

@ArtTurtle is an upvote bot run by @Artopiumdedicated to upvoting your art, music, fashion, video and books. Find out how you can get an upvote for every creative post you make by visitng @ArtTurtle and reading the latest report.

Thank you for your contribution.
While I liked the content of your contribution, I would still like to extend one advice for your upcoming contributions:

  • Put the commands separate from the texts, it is easier for the user to understand the steps that have to do.

Looking forward to your upcoming tutorials.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Hey @scipio
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Thanks #scipio, kindly visit my blog and give your valuable suggestions....!!!