Hey, check out my Modern Python Projects course. It's an extended version of this workshop!

Exercise 1

pyenv, pipx, and virtual environments

Let’s put what we just learned into action.

Here are some exercises where you will install and test the tools that we just talked about.

  1. Using the installation instructions from the “Python & packages” section, install pyenv and pipx. You can also install one of the virtual environment tools, but using either the built-in venv or pyenv-virtualenv is perfectly fine
  2. Install a newer version of Python using pyenv. If you are already using the latest version, try to install an older one.

    • Make sure you are comfortable with switching Python versions. Try to test the $ pyenv local and $ pyenv shell commands to see how they work.
  3. Create a new virtual environment.

    • Install a package inside the virtual environment. For example: $ pip install cowsay (it’s a silly package that prints some ASCII image together with whatever text you ask it to say)
    • Make the cow say something: $ cowsay hello world
    • List all installed packages with $ pip freeze
    • Deactivate the virtual environment: $ deactivate
    • Try to run cowsay again: $ cowsay hello again
    • Did it work? Do you know why?
    • Create another virtual environment. Make sure you are comfortable with activating/deactivating and switching between different virtual environments.
  4. Install a package with pipx. For example: $ pipx install glances (glances is a pretty useful tool to see what’s going on on your computer)

    • List the pipx packages with $ pipx list
    • Run the glances command: $ glances
    • Activate one of the virtual environments that you previously created and try to run glances again.
    • Uninstall the glances installed with pipx ($ pipx uninstall glances) and install it with pip INSIDE the virtual environment.
    • Deactivate the virtual environment and try to run glances again.
    • Did it work? Do you understand the difference between a package installed with pipx and a package installed in a virtualenv?