- Can you install multiple Python versions in Linux?
- How to do when a missing module error was thrown?
- Learn about search path to locate modules in Python
- Know the differences between Python 2 and 3
- How to resolve missing Python module
- ImportError: No module named 'encodings'
Multiple Python Installations
In our system, we have both Python 2 and 3 installed under /usr/bin as such:
$ pythonPython 2.4.3 (#1, Feb 24 2012, 13:04:26)[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2Type "help", "copyright", "credits" or "license" for more information.
$ python3Python 3.5.1 (default, Mar 24 2016, 20:01:47)[GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linuxType "help", "copyright", "credits" or "license" for more information.
In the rest of article, we will use Python 3.5.1 for illustration unless stated otherwise. Read section "Python 2 vs Python 3" to learn the differences between them.
Missing Python Module
Python module is a file (e.g., with suffixes like .py, .pyc, .pyo etc.):[13,17]Oftentimes, you could run into missing Python Module reported by ImportError module like:
- Containing Python definitions and statements
- Can be imported in a script or in an interactive instance of the interpreter
- Imported only once per interpreter session
- Simply for efficiency reasons
- If you change your modules, you must restart the interpreter
- If it’s just one module you want to test interactively, can also use importlib.reload(
$ python3.5Fatal Python error: Py_Initialize: Unable to get the locale encodingImportError: No module named 'encodings'
In such cases, you may need to fix sys.path to include missing library paths.
sys.path variable stores a list of strings that specifies the search path for modules. It is initialized from these locations:
- The directory containing the input script (or the current directory when no file is specified)
- PYTHONPATH (a list of directory names)
- With the same syntax as the shell variable PATH
A program is free to modify this list for its own purposes. Only strings and bytes should be added to sys.path; all other data types are ignored during import. See also Module site — This describes how to use .pth files to extend sys.path.
Python 2 vs 3
In this section, we will show you how to display sys.path value from the command line without entering interactive mode. To do that, we use a built-in module print. However, as noted below, there are syntax differences between Python 2 and Python 3 in the way of invoking it.
$ python -c 'import sys; print "\n".join(sys.path)'
$ python3 -c 'import sys; print("\n".join(sys.path))'/usr/lib/python35.zip/usr/lib/python3.5/usr/lib/python3.5/plat-linux/usr/lib/python3.5/lib-dynload/scratch/perf/.local/lib/python3.5/site-packages/usr/lib/python3.5/site-packages
Where Is a Python Module Located?
When a module, say, encodings is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named encodings.py in a list of directories given by the variable sys.path.
To find out where an imported module is located, you can use its attribute __file__. For example, module encodings is located under /usr/lib/python3.5 in our system:
>>> import encodings
- This error message means that you are attempting to use Python 3 to follow an example or run a program that uses the Python 2 print statement:
- Under Unix-like operating systems, when a script with a shebang is run as a program, the program loader parses the rest of the script's initial line as an interpreter directive; the specified interpreter program is run instead, passing to it as an argument the path that was initially used when attempting to run the script.