Import arbitrary python source file. (Python 3.3+)
Found a solution from importlib
test code.
Using importlib.machinery.SourceFileLoader:
>>> import importlib.machinery
>>> loader = importlib.machinery.SourceFileLoader('a_b', '/tmp/a-b.txt')
>>> mod = loader.load_module()
>>> mod
<module 'a_b' from '/tmp/a-b.txt'>
NOTE: only works in Python 3.3+.
UPDATE Loader.load_module
is deprecated since Python 3.4. Use Loader.exec_module
instead:
>>> import types
>>> import importlib.machinery
>>> loader = importlib.machinery.SourceFileLoader('a_b', '/tmp/a-b.txt')
>>> mod = types.ModuleType(loader.name)
>>> loader.exec_module(mod)
>>> mod
<module 'a_b'>
>>> import importlib.machinery
>>> import importlib.util
>>> loader = importlib.machinery.SourceFileLoader('a_b', '/tmp/a-b.txt')
>>> spec = importlib.util.spec_from_loader(loader.name, loader)
>>> mod = importlib.util.module_from_spec(spec)
>>> loader.exec_module(mod)
>>> mod
<module 'a_b' from '/tmp/a-b.txt'>
Python: Importing arbitrarily named source files using importlib
A solution to a similar question is posted here. Translated to my example:
import importlib.machinery, importlib.util
loader = importlib.machinery.SourceFileLoader('my_module', '/path/to/my_module')
spec = importlib.util.spec_from_loader(loader.name, loader)
my_module = importlib.util.module_from_spec(spec)
loader.exec_module(my_module)
Python 3.4: How to import a module given the full path?
This should work for all python files, regardless of file extension:
import importlib.machinery
modulename = importlib.machinery.SourceFileLoader('modulename','/Path/To/module.py').load_module()
This method was mentioned in the deprecation message in the imp.load_module
documentation.
Import arbitrary python source file. (Python 3.3+)
Found a solution from importlib
test code.
Using importlib.machinery.SourceFileLoader:
>>> import importlib.machinery
>>> loader = importlib.machinery.SourceFileLoader('a_b', '/tmp/a-b.txt')
>>> mod = loader.load_module()
>>> mod
<module 'a_b' from '/tmp/a-b.txt'>
NOTE: only works in Python 3.3+.
UPDATE Loader.load_module
is deprecated since Python 3.4. Use Loader.exec_module
instead:
>>> import types
>>> import importlib.machinery
>>> loader = importlib.machinery.SourceFileLoader('a_b', '/tmp/a-b.txt')
>>> mod = types.ModuleType(loader.name)
>>> loader.exec_module(mod)
>>> mod
<module 'a_b'>
>>> import importlib.machinery
>>> import importlib.util
>>> loader = importlib.machinery.SourceFileLoader('a_b', '/tmp/a-b.txt')
>>> spec = importlib.util.spec_from_loader(loader.name, loader)
>>> mod = importlib.util.module_from_spec(spec)
>>> loader.exec_module(mod)
>>> mod
<module 'a_b' from '/tmp/a-b.txt'>
How Can I Import A Python 3 Module With A Period In The Filename?
After looking through the CPython quite a lot and coming back to some other solutions (especially Import arbitrary python source file. (Python 3.3+)), I realized that I needed to pass the full path to my module. Here is the cross-platform, call-location-independent solution:
"""
import os, sys # For running from Notepad++ shortcut, etc
import importlib.machinery
program_1_4 = importlib.machinery.SourceFileLoader('program_1.4', os.path.join(sys.path[0], 'program_1.4.py')).load_module()
print(program_1_4)
program_1_4.main()
"""
How do I import a python file through a path?
You need to use importlib
to do what you want. Here is an example on how to import my_function
from a file:
import importlib
spec = importlib.util.spec_from_file_location('some_module_name', filename)
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
mod.my_function(1)
Probably the question is a duplicate of Import arbitrary python source file. (Python 3.3+)
Import Module System (Python Source Code Analysis)
find_and_load function can be found in Lib/importlib/*.py
.
Related Topics
(Z3Py) Checking All Solutions for Equation
Python Image Library Fails with Message "Decoder Jpeg Not Available" - Pil
How to Highlight Searched Queries in Result Page of Django Template
Comparison of R, Statmodels, Sklearn for a Classification Task with Logistic Regression
Urllib2 in Python Equivalent for Ruby
Get Protocol + Host Name from Url
Get Screenshot on Windows with Python
Subclassing Tuple with Multiple _Init_ Arguments
In Django, How Does One Filter a Queryset with Dynamic Field Lookups
Can Modules Have Properties the Same Way That Objects Can
Parameterized Queries with Psycopg2/Python Db-API and Postgresql
What Are Some Good Python Orm Solutions
CSS Not Rendered by Pisa's PDF Generation in Django
How to Get Access of Individual Trees of a Xgboost Model in Python /R
Rally APIs: How to Copy Test Folder and Member Test Cases