arrow-left

All pages
gitbookPowered by GitBook
1 of 1

Loading...

Install Python

hashtag
Installing Python 3

Brian "Beej Jorgensen" Hall edited this page on May 29, 2020 · 1 revisionarrow-up-right

NOTE: pipenv is optional! We don't use it in CS. But it's a neat package manager if you get into more complex Python projects. It can be a headache of an install for some people. You can safely ignore anything about pipenv below if you don't want to mess with it.

We'll want to install Python 3 (version 3.x), which is the runtime for the language itself. The runtime is what allows you to execute Python code and files by typing python [file_or_code_to_execute] in your terminal. You can also open up a Python REPL (Read-Eval-Print Loop) to quickly and easily mess around with Python code once you've gotten the runtime installed. If you recall how Node let's you execute and run JavaScript code locally on your machine instead of having to rely on the browser, the Python runtime pretty much let's you do the same thing but with Python code.

Additionally, we'll be talking about how to install the (optional) pipenv virtual environment manager. Think of it as the npm of Python (though pipenv is also capable of performing a bunch of other tasks as well, most notably running your Python projects in an isolated virtual environment).

hashtag
Note for Anaconda users

Unfortunately, we haven't found a way to get Anaconda to play nicely with pipenv. If you get them working together, please let your instructor know how you did it.

hashtag
Testing the Install

If you can run python or python3 and see a 3.7 or later version, you're good to go:

or on some systems, Python 3 is just python:

And optionally try pipenv:

Otherwise, keep reading. :)

hashtag
macOS

While macOS comes with Python 2, we need to get Python 3 in there as well.

If you don't have Brew installed, .

Use Brew to install Python and pipenv at the Terminal prompt:

hashtag
Windows

Note: Git Bash doesn't seem to cooperate if you're trying to install Python on Windows. Try another terminal like Powershell.

Recommend updating Windows to the latest version.

hashtag
Windows Store

Python 3 is in the Windows Store and can be installed from there.

hashtag
Official Binaries

When installing the official package, be sure to check the

checkbox!!

hashtag
Pipenv

This is what worked for Beej. YMMV.

  1. Install Python, as per above.

  2. Bring up a shell (cmd.exe or Powershell)

  3. Run py -m pip

Pipenv official instructions

hashtag
Chocolatey

hashtag
WSL

If you're running Windows 10+, you might want to install the Windows Subsystem for Linux. This gives you a mini, integrated Linux system inside Windows. You then install and use Python from there.

  1. Update Windows to latest if you haven't already.

  2. .

  3. Go to the Microsoft store and install Ubuntu.

If you've installed VS Code, add the "Remote WSL" extension. This way you can run code from within Ubuntu.

In the Ubuntu shell, you can run explorer.exe . in any directory to open a Windows Explorer window in that directory.

Also in Windows Explorer, you can put \\wsl$ in the URL bar to see your Ubuntu drive. (If it doesn't show up, relaunch your Ubuntu shell.)

If you run into trouble with the above, try the following:

  1. Open cmd.exe as an administrator and start bash with bash

    1. Type Python -V' and 'Python3 -V

Run py -m pip install --user pipenv

At this point, you should be able to always run pipenv with py -m pipenv, but that's a little inconvenient. Read on for making it easier.

  • You'll see a message like this in the pipenv install output, but with a slightly different path:

  • Select that path (not including any quotes around it), and copy it

  • Go to the Start menu, type "environment" and run the program Edit Environment Variables

  • In the System Properties popup, hit the Advanced tab, then Environment Variables

  • On the list of environment variables, doubleclick Path

  • Click New

  • Paste that path from step 5 into the new entry slot. Make sure there aren't any quotes around it.

  • Click OK, OK, OK.

  • Relaunch any shells you have open.

  • Now you should be able to just run pip and pipenv in Powershell without putting py -m in front of it.

  • Run Ubuntu to get a bash shell.
  • Make a new username and password. This is completely separate from your Windows username and password, but I made mine the same so that I wouldn't forget.

  • Upgrade the Ubuntu system. Run:

    Give your newly-entered password if prompted.

  • Running python3 --version should give you 3.6 or higher.

  • Run pip install pipenv.

  • If one of these responds with Python 3.6.8 use that command from now on
  • If neither response is Python 3.6.8 but one is a higher version of Python, this means one of two things

    1. If you have manually installed a higher version of Python, we recommend uninstalling it

    2. If you have not, it is possible that Microsoft has updated WSL and you will need to adjust these instructions to accommodate

  • Otherwise, update Ubuntu:

    1. sudo apt-get update

    2. sudo apt-get upgrade

  • Repeat 2.1 above to determine if you should use Python or Python3 when using Python. Note: inside the shell, you will always use Python as the command.

  • Make sure pip is installed for Python 3

    1. pip --version and pip3 --version. One of these needs to respond with a version that has a version of Python 3 at the end of the line.

    2. If you only have it for 2.7, you will need to install for 3 with:

      1. sudo apt update && sudo apt upgrade

      2. sudo apt install python3-pip

    3. Check versions and commands again. You will likely need to use pip3 for the next step, but it's possible it may be just pip. Use the one with the version associated with Python 3.6.8

  • Make sure pipenv is installed for Python 3 python3 -m pipenv --version

    1. If not, install pipenv:

      1. sudo apt update && sudo apt upgrade (if you didn't just do this above)

      2. pip3 install --user pipenv

    2. Check the version again

  • Try pipenv shell. If this fails, make sure that every reference in the error refers to Python 3.6. If not, review the above steps

    1. If the error does refer to 3.6:

      1. Confirm that python --version refers to 2.7.something

      2. Confirm that /usr/bin/python3 --version refers to 3.6.8

      3. pipenv --three --python=which python3`` NOTE that there are backticks (`) around which python3

      4. This should create the shell forcing it to use 3.6.8

  • follow the instructions on the brew websitearrow-up-right
    Official Packagearrow-up-right
    Install pipenv per these instructionsarrow-up-right
    Install Chocolateyarrow-up-right
    Install Python 3 with Chocolateyarrow-up-right
    Install pipenv per these instructionsarrow-up-right
    Install WSL from herearrow-up-right
    add C:\Users\username\AppData\Roaming\Python\Python38\Scripts to your path
    sudo apt-get update
    sudo apt-get upgrade
    $ python3 --version
    Python 3.6.5
    $ python --version
    Python 3.6.5
    $ pipenv --version
    pipenv, version [some remotely recent date, probably]
    brew install python pipenv
    [ ] Add to PATH