Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1

where the standard Python library modules’ .py files, are located for this Python version. You
looked at this directory back in the “Introducing Module Concepts” section of this hour.


Did You Know: The Path
Many programming languages and operating systems use the term path. A path is a list
of directories a program searches for other programs, libraries, components, and so
on. For modules, Python searches the path directories for modules (actually, it
searches for their .py files) that have been requested to be imported.

Following standards is always good form. Also, it protects any modules you create from being
unintentionally removed. For example, if you put your custom modules with the other standard Python
modules in /usr/lib/pythonversion, when the Python software is upgraded, your modules
could be deleted.


Table 13.1 shows the standard Python module directories for a Debian-based operating system.
Remember from Hour 2 that Raspbian is a Debian-based Linux distribution.


TABLE 13.1 Standard Python Module Directories

According to Table 13.1, when you are ready to move your module into a production directory, it
needs to go into the /usr/local/lib/pythonversion/site-packages directory. The
steps needed to make this move have to be performed at Raspbian’s dash shell. Line 1 of Listing
13.11 checks the directory /usr/local/lib/python3.2 to see if the site-packages
subdirectory exists. In this case, it does not. Only the dist-packages subdirectory exists, as you
see on line 2. Therefore, the site-packages subdirectory is created on line 3, using the sudo
and mkdir commands you learned about in Hour 2. If your system already has the site-
packages subdirectory, you do not need to use those commands.


LISTING 13.11 Copying arith.py to the Production Directory


Click here to view code image


1: $ ls /usr/local/lib/python3.2/
2: dist-packages
3: $ sudo mkdir /usr/local/lib/python3.2/site-packages
4: $
5: $ cd /home/pi/py3prog
6: $
7: $ sudo cp arith.py /usr/local/lib/python3.2/site-packages/
8: $ cd
9: $
Free download pdf