
C H A P T E R 1 2
A N I N T R O D U C T I O N T O P Y T H O N
179
Making Python Programs Executable
Normally, the only way to run a Python program is to tell the Python software to open the
file. With the shebang line at the top of the file, however, it’s possible to execute the file
directly without having to call Python first. This can be a useful way of making your own
tools that can be executed at the terminal: once copied into a location in the system’s
$PATH
environment variable, the Python program can be called simply by typing its name.
First, you need to tell Linux that the Python file should be marked as executable—an attribute
that means the file is a program. To protect the system from malware being downloaded
from the Internet this attribute isn’t automatically set, since only files that are marked as exe-
cutable will run. To make the
helloworld.py
file executable, use the
chmod
command
(described in detail in Chapter 3, “Linux System Administration”) by typing the following:
chmod +x helloworld.py
Now try running the program directly by typing the following:
./helloworld.py
Despite the fact that you didn’t call the Python program, the
helloworld.py
program
should run just the same as if you’d typed
python helloworld.py
. The program can
only be run by calling it with its full location—
/home/pi/helloworld.py
—or from the
current directory by using
./
as the location. To make the file accessible in the same way
as any other terminal command, it needs to be copied to
/usr/local/bin
with the fol-
lowing command:
sudo cp helloworld.py /usr/local/bin/
The
sudo
prefix is required because, for security reasons, non-privileged users cannot write to
the
/usr/local/bin
directory. With the
helloworld.py
file located in
/usr/local/bin
,
which is included in the
$PATH
variable, it can be executed from any directory by simply typing
its name. Try changing to a different directory, and then run the program by typing the following:
helloworld.py
To make your custom-made programs seem more like native utilities, you can rename them
to remove the
.py
file extension. To change the
helloworld.py
program in this way, just
type the following line at the terminal as a single line:
sudo mv /usr/local/bin/helloworld.py
↵
/usr/local/bin/helloworld
Once renamed, the program can be run simply by typing
helloworld
at the terminal or
console.
Содержание A
Страница 1: ......
Страница 2: ......
Страница 3: ...Raspberry Pi User Guide 2nd Edition...
Страница 4: ......
Страница 5: ...Raspberry Pi User Guide 2nd Edition Eben Upton and Gareth Halfacree...
Страница 10: ......
Страница 26: ...R A S P B E R R Y P I U S E R G U I D E S E C O N D E D I T I O N 10...
Страница 28: ......
Страница 29: ...Chapter 1 Meet the Raspberry Pi...
Страница 37: ...Chapter 2 Getting Started with the Raspberry Pi...
Страница 56: ......
Страница 57: ...Chapter 3 Linux System Administration...
Страница 79: ...Chapter 4 Troubleshooting...
Страница 89: ...Chapter 5 Network Configuration...
Страница 109: ...Chapter 6 The Raspberry Pi Software Configuration Tool...
Страница 122: ......
Страница 123: ...Chapter 7 Advanced Raspberry Pi Configuration...
Страница 140: ......
Страница 141: ...Chapter 8 The Pi as a Home Theatre PC...
Страница 151: ...Chapter 9 The Pi as a Productivity Machine...
Страница 159: ...C H A P T E R 9 T H E P I A S A P R O D U C T I V I T Y M A C H I N E 143 Figure 9 4 Exporting a file from The Gimp...
Страница 160: ......
Страница 161: ...Chapter 10 The Pi as a Web Server...
Страница 171: ...Part III Programming with the Raspberry Pi Chapter 11 An Introduction to Scratch Chapter 12 An Introduction to Python...
Страница 172: ......
Страница 173: ...Chapter 11 An Introduction to Scratch...
Страница 189: ...Chapter 12 An Introduction to Python...
Страница 216: ......
Страница 218: ......
Страница 219: ...Chapter 13 Learning to Hack Hardware...
Страница 234: ......
Страница 235: ...Chapter 14 The GPIO Port...
Страница 249: ...Chapter 15 The Raspberry Pi Camera Module...
Страница 265: ...Chapter 16 Add on Boards...
Страница 279: ...Part V Appendixes Appendix A Python Recipes Appendix B Camera Module Quick Reference Appendix C HDMI Display Modes...
Страница 280: ......
Страница 281: ...Appendix A Python Recipes...
Страница 287: ...Appendix B Raspberry Pi Camera Module Quick Reference...
Страница 293: ...Appendix C HDMI Display Modes...