Friday, March 25, 2016

How-to: When a Missing Python Module Error Was Thrown

When updating Python from 2 to 3, you may want to get familiar with the following topics first:
  1. Can you install multiple Python versions in Linux?
  2. How to do when a missing module error was thrown?
  3. Learn about search path to locate modules in Python
  4. Know the differences between Python 2 and 3[1]
  5. How to resolve missing Python module 
    • ImportError: No module named 'encodings'
  6. Where is a specific Python module located?

Multiple Python Installations


In our system, we have both Python 2 and 3 installed under /usr/bin as such:
  • /usr/bin/python
  • /usr/bin/python3
To choose a specific version to use in your python scripts, you can specify shebang (or hashbang) as follows:
#!/usr/bin/python3

Python2

$ python
Python 2.4.3 (#1, Feb 24 2012, 13:04:26)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
Type "help", "copyright", "credits" or "license" for more information.


Python3

$ python3
Python 3.5.1 (default, Mar 24 2016, 20:01:47)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-52)] on linux
Type "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]
  • 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().[14]
Oftentimes, you could run into missing Python Module reported by ImportError module like:

$ python3.5
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
In such cases, you may need to fix sys.path to include missing library paths.

sys.path


sys.path variable stores a list of strings that specifies the search path for modules. It is initialized from these locations:
  1. The directory containing the input script (or the current directory when no file is specified)
  2. PYTHONPATH (a list of directory names)
    • With the same syntax as the shell variable PATH
  3. The installation-dependent default

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 2

$ python -c 'import sys; print "\n".join(sys.path)'
/usr/lib64/python24.zip
/usr/lib64/python2.4
/usr/lib64/python2.4/plat-linux2
/usr/lib64/python2.4/lib-tk
/usr/lib64/python2.4/lib-dynload
/usr/lib64/python2.4/site-packages
/usr/lib64/python2.4/site-packages/Numeric
/usr/lib64/python2.4/site-packages/PIL
/usr/lib64/python2.4/site-packages/gtk-2.0
/usr/lib/python2.4/site-packages

Python 3

$ 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__.[11]  For example, module encodings is located under /usr/lib/python3.5 in our system:

$ python3
<snipped>
>>> import encodings
>>> print(encodings.__file__)
/usr/lib/python3.5/encodings/__init__.py

References

  1. What does “SyntaxError: Missing parentheses in call to 'print'” mean in Python?
    • 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:
  2. Install / Update Python 3.5.0 at Linux machine. (Youtube)
  3. Python 3.5.1 
  4. Python Module
  5. upgrade Python to 2.7.2
  6. How can I troubleshoot Python “Could not find platform independent libraries
  7. Py_Initialize: Unable to get the locale encoding in OpenSuse 12.3
  8. Environment Variables (Python)
  9. Python script header
  10. Standard modules (Python)
  11. How do I find the location of Python module sources?
  12. sys module — System-specific parameters and functions
  13. What do the python file extensions, .pyc .pyd .pyo stand for?
  14. How do I unload (reload) a Python module?
  15. Purpose of #!/usr/bin/python3 (important)
  16. shebang (or hashbang)
    • 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.
  17. Importing Python Modules



No comments: