How python finds its modules
Strictly taken, a module is a single python file, while a package is a folder containing python files, accompanied by a (can be empty) file named __init__.py, to tell python it is a package to import modules from. In both cases, modules need their .py extension, but importing them is done without (see further below).
By default, Python looks for its modules and packages in $PYTHONPATH.
To find out what is included in $PYTHONPATH, run the following code in python (3):
import sys
print(sys.path)
How to add a directory
Occasionally
From within a python file, you can add path(s) occasionally to the default path by adding the following lines in the head section of your python application or script:
import sys
sys.path.insert(0, "/path/to/your/package_or_module")
For example:
if I have a folder: /home/myname/pythonfiles, and I want to import the file module_1.py, located in that directory, I add this to the head section of my code:
import sys
sys.path.insert(0, "/home/myname/pythonfiles")
And I can simply import the file module_1.py by:
import module_1
When I create a package and want to import module(s) from the package, I need to create a folder in $PYTHONPATH, containing the modules, accompanied by a (can be empty) file called __init__.py
For example:
To import from a package (folder) called my_package in /home/myname/pythonfiles , add the /home/myname/pythonfiles path to your $PYTHONPATH, like in example 1, and import the module called module_2.py (inside the package folder) simply with: `
from <packagename> import module_2
Adding directories to $PYTHONPATH permanently:
Add the following line to your ~/.profile file.
export PYTHONPATH=$PYTHONPATH:/path/you/want/to/add
Subdirectories
From within a package, subdirectories are not included just like that; you need to "chain" the directories. To import a module module_3.py, inside folder subfolder inside folder packagename:
import packagename.subfolder.module_3
Given the fact that all subfolders in the package include their own __init__.py file.
When a module is in the same directory as the script or application
There is no need to insert the path to a module when it is in the same directory as the script or application, it is automatically added.
Example:
If I have a folder, containing script.py and module.py, I can simply import the module by:
import module
Answer from Jacob Vlijm on askubuntu.comWhy is the first element in Python's sys.path an empty string? - Stack Overflow
sys.path[0] Is Set Differently From the Rest of sys.path
python - Why does sys.path start with an empty string? - Stack Overflow
python - Is it safe to use sys.path[0] in code when pointing to a file location - Stack Overflow
How python finds its modules
Strictly taken, a module is a single python file, while a package is a folder containing python files, accompanied by a (can be empty) file named __init__.py, to tell python it is a package to import modules from. In both cases, modules need their .py extension, but importing them is done without (see further below).
By default, Python looks for its modules and packages in $PYTHONPATH.
To find out what is included in $PYTHONPATH, run the following code in python (3):
import sys
print(sys.path)
How to add a directory
Occasionally
From within a python file, you can add path(s) occasionally to the default path by adding the following lines in the head section of your python application or script:
import sys
sys.path.insert(0, "/path/to/your/package_or_module")
For example:
if I have a folder: /home/myname/pythonfiles, and I want to import the file module_1.py, located in that directory, I add this to the head section of my code:
import sys
sys.path.insert(0, "/home/myname/pythonfiles")
And I can simply import the file module_1.py by:
import module_1
When I create a package and want to import module(s) from the package, I need to create a folder in $PYTHONPATH, containing the modules, accompanied by a (can be empty) file called __init__.py
For example:
To import from a package (folder) called my_package in /home/myname/pythonfiles , add the /home/myname/pythonfiles path to your $PYTHONPATH, like in example 1, and import the module called module_2.py (inside the package folder) simply with: `
from <packagename> import module_2
Adding directories to $PYTHONPATH permanently:
Add the following line to your ~/.profile file.
export PYTHONPATH=$PYTHONPATH:/path/you/want/to/add
Subdirectories
From within a package, subdirectories are not included just like that; you need to "chain" the directories. To import a module module_3.py, inside folder subfolder inside folder packagename:
import packagename.subfolder.module_3
Given the fact that all subfolders in the package include their own __init__.py file.
When a module is in the same directory as the script or application
There is no need to insert the path to a module when it is in the same directory as the script or application, it is automatically added.
Example:
If I have a folder, containing script.py and module.py, I can simply import the module by:
import module
The correct way to use relative import is:
from ..Common import foo1
And you also need a __init__.py in all your folders.
sys.path[0] is an entry created by the Python executable to refer to the directory of the script being run. If no script is being run, e.g. the REPL has been invoked directly, an empty entry representing the current directory is added.
The first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter.
If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first.
As per documentation here
As per the documents:
A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.
As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first. Notice that the script directory is inserted before the entries inserted as a result of
PYTHONPATH.
A program is free to modify this list for its own purposes.
So sys.path is basically the path of the current directory and the paths resulting from the PYTHONPATH environmental variable.
If I open up the python repl, sys.path outputs:
['', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/home/mike/.local/lib/python3.4/site-packages', '/usr/local/lib/python3.4/dist-packages', '/usr/lib/python3/dist-packages']
However, if I echo PYTHONPATH on the commandline:
echo $PYTHONPATH
Nothing outputs. If the PYTHONPATH environmental variable isn't set then how does python know which paths to use?