- 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[1]
- How to resolve missing Python module
- ImportError: No module named 'encodings'
- 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
#!/usr/bin/python3
Python2
$ 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.
Python3
$ 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(
).[14]
$ 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
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
- 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
- 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:
- Install / Update Python 3.5.0 at Linux machine. (Youtube)
- Python 3.5.1
- Python Module
- upgrade Python to 2.7.2
- How can I troubleshoot Python “Could not find platform independent libraries
” - Py_Initialize: Unable to get the locale encoding in OpenSuse 12.3
- Environment Variables (Python)
- Python script header
- Standard modules (Python)
- How do I find the location of Python module sources?
- sys module — System-specific parameters and functions
- What do the python file extensions, .pyc .pyd .pyo stand for?
- How do I unload (reload) a Python module?
- Purpose of #!/usr/bin/python3 (important)
- 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.
- Importing Python Modules
No comments:
Post a Comment