DEV Community

theBridge2
theBridge2

Posted on • Updated on

Python fundamentals (2 of 2): How to install packages and deal with PATH variable.

If you missed it, consider first checking out my first part Python fundamentals part 1 - installing and running

Package installation is a fairly straightforward process thanks to the Python's Preferred Installer Program or pip for short.

However, it seems most explanations assume you understand how to get the PATH variable which is required to get anything to run on the command line. Lets cover basic install first then the PATH variable.

Useful install related commands

Command Purpose
pip install packageName installs some package
pip uninstall packageName uninstalls some package
pip list lists packages installed
packageName --version displays version of installed package
pip install --upgrade pip upgrade the pip
pip show packageName Prints useful list of info about package including where the package is installed on your computer

Installing a package

Let's start with the well known numpy library used for mathematical operations.

import numpy as np
print("test")
Enter fullscreen mode Exit fullscreen mode

If this fails, you will get the error: "No module named 'numpy'"

The simple solution is to install the module. Every site everywhere explains this. The command is:

pip3 install numpy
Enter fullscreen mode Exit fullscreen mode

Note that the '3' in pip3 is for python 3.

To prove you installed it type the command 'pip3 list' and you will see packages that are installed:

Image description

Installing a package to be used on command line: pytest

I'm going to be using the python package pytest since it is a harder example and will require an understanding of the PATH variable.

Here is the basic install command for python to be run in the command prompt:

pip3 install pytest
Enter fullscreen mode Exit fullscreen mode

Then to confirm it works you can type:

pytest -h
Enter fullscreen mode Exit fullscreen mode

If this is the first time you are doing this, more than likely this will fail. And you will get the same error trying to run it as you would if it wasn't installed at all.

Image description

Go back and take a look at text printed from the install messages and likely you will find something that looks like this:

Image description

which tells you something about a potential PATH issue.

What is the PATH variable?

Command line PATH variable

PATH is an environment variable. Environment variables are variables set outside of your code.
For python packages to be able to be used on the command line, it must know where they are. Generally python packages are installed in a scripts subdirectory next to your python.exe. See below for more details.

When you run the command line in any OS, it will not inherently understand what python or other commands not embedded in the OS are. In order to tell the command line what python is, you will need to add the executable file for python to command line tool's PATH variable.

Windows

echo %PATH%  
Enter fullscreen mode Exit fullscreen mode

Linux

echo ${PATH}
Enter fullscreen mode Exit fullscreen mode

This prints all of the places the command line will look for commands.

When the command line looks for these command definitions it will look through the list of paths and stop when it is found. So if you have more than one version of python installed, it will find the first version installed and use that one. If you have more than one version of python installed and want explicit control over which one it uses, you should use a python virtual environment.

How do I fix my PATH issue?

To solve your PATH issue for your python command like pytest that needs to be run on the command line, follow these brief steps. For steps 1 & 2 for more detail I found this video to be useful: https://www.youtube.com/watch?v=3J96_vyfx8Y

Here is another good reference: https://realpython.com/add-python-to-path/

Steps:

1) Find your installed package location. Generally it is next to your python.exe location and then navigate into the subdirectory /scripts. Take a look at the PATH error in yellow above. Sometimes on windows it is installed to your roaming directory.

2) Copy and paste this pip path folder name to your environment variables both in the user and system variables section under PATH
3) Restart your command prompt to get the new system variables update
4) >pip uninstall pytest
5) >pip install pytest
6) pytest -h # should now work!

After fixing the path issue, now we get a wall of text in response to our pytest -h in the command line instead of our error.

Image description

I installed the package but it still isn't found!?

Type 'pip3 list -v' to see where the packages are installed.

Image description

To figure out where python is actually looking for imports use this code:

import sys
print(sys.path)
Enter fullscreen mode Exit fullscreen mode

If the list does not include your package install path you can either reinstall it to the right location or modify your path with sys.path.append. Sys.path.append is not best practice, but it is simplest for a new python programmer to get started with.

import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))) "/../someFolder)
# where __file__ refers to the current file and can be replaced with a relative path
Enter fullscreen mode Exit fullscreen mode

Best practice is to use some kind of virtual environment like venv or docker.

My next article will be on using a virtual environment to manage dependencies. Until then, you can use this for reference: https://realpython.com/python-virtual-environments-a-primer/

Top comments (0)