Import py file in Python
In Python programming language, a module is a single layer of block of Python code that can be loaded and used by importing into other Python block of code. A module in python is a combination of definitions which include functions and constants and statements that refer to those definitions. When the module code is written, and it is written in such a way that it can reused in any type of other module by importing it.
The simplest way to create a python module is by creating a file with a filename which is having the .py extension. The import statements wont work if the given extension is not followed and rather than we need to import another library from python i.e., importlib and writing the code in it.
To understand the standard usage of import, let's create a file with the following extension .py with the name hello.py and deploy the code as follow.
Code:
def hello_world()
print( ‘python hello’ )
Now, there is no need of writing always the given code for the desired required output because if we need it, we can just simply import the given module rather than writing the whole code again. The main feature of this is we can change a single line of code inside the module rather than changing and displaying this message in the hello.py module.
Importing File in the same Directory:
We have two python files in the same directories namely following as:
Folder name: tutorilandexample
Python files:
- hello.py
- Python2.py
The following files contain the code as the hello.py contains the above code and to recall the hello_world function from python2.py, we can deploy the new code in python2.py.
Code:
import hello
hello.hello_world()
here, in the import statements we used a name, and it is simply the modules filename without .py extension to it.
Import a File in a Subdirectory:
Importing a module in sub directories is possible only in the python versions python 3.3 and above versions. If the version is older than 3.3, we need to follow some steps by creating another sub directory and importing the file in different directory.
Python version 3.3 above, we don’t need to create another sub-dir.
We can directly import it into python2.py. let’s create a sub directory as subdir.
Code:
import subdir.hello
subdir.hello.say_hello ()
NOTE:
While declaring the system path, we separate the system components using / (or) \ but in python import we separate components using a dot symbol (.)
We can also import module as declaring as any variable by using import as.
Code:
import subdir.hello as m
m.say_hello ()
here, m is chosen as importing subdir module.
Importing a file from multi-level sub directories is also possible not only from single level.
Example:
Now, we create two level of directories namely as pythonA & pythonB and let’s assume we have the following level as.
Code:
tutorialandexample/
pythonA/
pythonB/
hello.py
python2.py
and for this to access the .py files we can deploy code as:
Code:
import pythonA.pythonB.hello as hello
hello.say_hello ()
Import a File in a Different Directory:
Now let’s try with a other case by moving the module to another directory that is not in the path tree. But python itself by default looks and searches the files in all the directories and all the standard locations defined in system path.
We need use append function to write in our old module.
Code:
import sys
sys.path. append (‘/pythonA/pythonB’ )
import hello
hello.say_hello ()
To append we need to use absolute path because the file is present in different directory so we need to define the path from the root of the file system rather than using the relative path. We can also use __file__ to get the full file.
Code:
import os
import sys
python2_dir = os. path.dirname(__file__ )
hello_dir = os. path.join( python2_dir, '..', 'pythonA', 'pythonB' )
sys. path. append(hello_dir)
import hello
hello.say_hello ()
Importing a file with no .py extension:
To utilize this function body, we need have the python version 3.4 and above because this version provides a built-in library known as importlib. This allows us to open and load any file even if it is a python module with no .py extension
Now, we remove extensions from the python files and proceed with the importlib library.
import importlib.machinery
import importlib.util
loader = importlib. machinery.SourceFileLoader( 'hello', '/pythonA/pythonB/hello' )
spec = importlib.util.spec_from_loader( 'hello', loader )
hello = importlib.util.module_from_spec( spec )
loader.exec_module( hello )
# Use hello
hello.say_hello()