Beginning Programming with Python For Dummies
Book image
Explore Book Buy On Amazon
In the Python programming language, to use code in a package, Python must be able to locate the package and load it into memory. The location information is stored as paths within Python. Whenever you request that Python import a package, Python looks at all the files in its list of paths to find it. The path information comes from three sources.

Path information sources

  • Environment variables: Python environment variables, such as PYTHONPATH, tell Python where to find modules on disk.

  • Current directory: You can change the current Python directory so that it can locate any modules used by your application.

  • Default directories: Even when you don’t define any environment variables and the current directory doesn’t yield any usable modules, Python can still find its own libraries in the set of default directories that are included as part of its own path information.

How to find path information

It’s helpful to know the current path information because the lack of a path can cause your application to fail. The following steps demonstrate how you can obtain path information:
  1. Open the Python Shell.

    You see the Python Shell window appear.

  2. Type import sys and press Enter.

  3. Type for p in sys.path: print(p) in a new cell and click Run Cell

    You see a listing of the path information, as shown in the figure below. Your listing may be different from the one shown in the figure, depending on your platform, the version of Python you have installed, and the Python features you have installed.

    Finding packages on disk in Python

Another way to find paths

The sys.path attribute is reliable but may not always contain every path that Python can see. If you don’t see a needed path, you can always check in another place that Python looks for information. The following steps show how to perform this task:
  1. In a new cell, type import os and press Enter.

  2. Type os.environ['PYTHONPATH'].split(os.pathsep) and press Enter.

    When you have a PYTHONPATH environment variable defined, you see a list of paths, as shown in the figure below. However, if you don’t have the environment variable defined, you see an error message instead.

    Requesting info about environment variables in Python

    The sys.path attribute doesn’t include the split() function, which is why the example uses a for loop with it. However, the os.environ['PYTHONPATH'] attribute does include the split() function, so you can use it to create a list of individual paths.

    You must provide split() with a value to look for in splitting a list of items. The os.pathsep constant (a variable that has one, unchangeable, defined value) defines the path separator for the current platform so that you can use the same code on any platform that supports Python.

You can also add and remove items from sys.path. For example, if you want to add the current working directory to the list of packages, you type sys.path.append(os.getcwd()) and press Enter.

When you list the sys.path contents again, you see that the new entry is added to the end of the list. Likewise, when you want to remove an entry you type sys.path.remove(os.getcwd()) and press Enter. The addition is present only during the current session.

About This Article

This article is from the book:

About the book author:

John Paul Mueller is a freelance author and technical editor with more than 107 books and 600 articles to his credit. His subjects range from networking and artificial intelligence to database management and heads-down programming. He also consults and writes certification exams. Visit his website at http://www.johnmuellerbooks.com/.

This article can be found in the category: