Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do these python `import` statements mean?

Tags:

python

At the beginning of a python script, there are some import statements. Could someone explain what they imply?

import getopt  
import os  
import re   
import string  
import sys  
import getpass  
import urllib  
import subprocess
like image 884
Navaneeth Sen Avatar asked Apr 09 '11 13:04

Navaneeth Sen


2 Answers

The import statements are similar (but different) to the #include statements in C: they allow you to use functions defined elsewhere (either in a standard module, or your own).

For example, module sys allows you to do this:

import sys
# ... somewhere down in the file
sys.exit(0)

Which would terminate your program. Note that you did not have to write any code for the exit() function, but it is defined within the standard sys module that ships with the interpreter.

Any Python tutorial should explain this. For example, this.

like image 66
evgeny Avatar answered Nov 04 '22 20:11

evgeny


It's importing modules (like libraries). When imported in this manner you'll find in the code calls to functions of this kind < module_name >.< function >

To know what each module do and offers, look at the documentation; a quick googling "python " should land you in the right place.

like image 37
garph0 Avatar answered Nov 04 '22 21:11

garph0