The Python Interpreter and Importing Modules
Python is an interpreted language. The interpreter goes through the source code line by line. Each line is transformed into machine code and then executed by the computer’s hardware. If it encounters an error, it stops. The interpreter acts as a bridge between human-readable source code and computer hardware. It is an executable file.
To check if the Python interpreter is installed
If it is, you’ll get something back like
[ The interpreter executable file is called python
. The path to its directory must be listed in the environment variable
otherwise the above command errors. Even if the executable python
is in the working directory, it errors, as the correct command then is
If it errors, this does not mean Python is not installed. It could just be that its directory is not in $PATH
. This is unlikely though, as usually by default Python is installed in /usr/bin
, /usr/local/bin
, etc. which is usually in $PATH
].
If the command works, how to tell the executable’s location? We know it is in $PATH
, but typically there are many paths listed there. Use the which
command
If you just type
this brings up the Python interactive shell
a handy tool for testing out code snippets directly in the terminal.
Normally though, your Python code is in a file, written using a text editor, e.g. Sublime. To get the interpreter to translate the code in the file into machine code
or a relative path can be used, e.g. if the script is in the working directory
As well as translating your source code into machine code, the interpreter ensures your computer’s hardware runs the machine code.
Much of Python’s utility comes from pre-installed standard libraries, e.g.
as well as numerous third-party libraries, e.g. pandas. These libraries are located in one of the paths in sys.path
and are imported via the import
keyword. If you wish to import
files containing your own Python code, one way to do this is by adding the path of your file to sys.path
via the $PYTHONPATH
environment variable.