For Python 3.5+ use:
import importlib.util spec = importlib.util.spec_from_file_location("module.name", "/path/to/file.py") foo = importlib.util.module_from_spec(spec) spec.loader.exec_module(foo) foo.MyClass()
For Python 3.3 and 3.4 use:
from importlib.machinery import SourceFileLoader foo = SourceFileLoader("module.name", "/path/to/file.py").load_module() foo.MyClass()
(Although this has been deprecated in Python 3.4.)
For Python 2 use:
import imp foo = imp.load_source('module.name', '/path/to/file.py') foo.MyClass()
There are equivalent convenience functions for compiled Python files and DLLs.
See also http://bugs.python.org/issue21436.
Related Posts:
- How to import other Python files?
- ImportError: libSM.so.6: cannot open shared object file: No such file or directory
- Python: ‘ModuleNotFoundError’ when trying to import module from imported package
- Relative imports – ModuleNotFoundError: No module named x
- Relative imports in Python 3
- Is “from matplotlib import pyplot as plt” == “import matplotlib.pyplot as plt”?
- Python error “ImportError: No module named”
- Python error “ImportError: No module named”
- What does if __name__ == “__main__”: do?
- ImportError: No module named requests
- Importing modules from parent folder
- `from … import` vs `import .` [duplicate]
- Importing modules from parent folder
- Importing files from different folder
- Importing files from different folder
- ImportError: Cannot import name X
- How to import the class within the same directory or sub directory?
- How do I unload (reload) a Python module?
- How to import the class within the same directory or sub directory?
- How do I find out my PYTHONPATH using Python?
- django import error – No module named core.management
- How to fix “Attempted relative import in non-package” even with __init__.py
- How to fix “Attempted relative import in non-package” even with __init__.py
- Why does using from __future__ import print_function breaks Python2-style print?
- Importing variables from another file in Python
- ImportError: No module named ‘cv2’ Python3
- NameError: name ‘requests’ is not defined
- How to load all modules in a folder?
- How to set NotebookApp.iopub_data_rate_limit and others NotebookApp settings in JupyterHub?
- Import a file from a subdirectory?
- Installing lxml module in python
- ModuleNotFoundError: What does it mean __main__ is not a package?
- Import file from parent directory?
- Python: OSError: [Errno 2] No such file or directory: ”
- How do I lowercase a string in Python?
- Difference between del, remove, and pop on lists
- How to use the pass statement
- How do you round UP a number?
- How do I update\upgrade pip itself from inside my virtual environment?
- ImportError: DLL load failed: The specified module could not be found
- Replacements for switch statement in Python?
- Correct way to write line to file?
- Depth-first search (DFS) code in python
- How to use Python to execute a cURL command?
- What does %s mean in a python format string?
- Redirecting to URL in Flask
- TypeError: ‘float’ object is not callable
- Plotting a 2D heatmap with Matplotlib
- Purpose of __repr__ method?
- json.dumps vs flask.jsonify
- How to print like printf in Python3?
- “Defaulting to user installation because normal site-packages is not writeable” python message
- How to install pytorch in Anaconda with conda or pip?
- Replace values in list using Python
- Trouble updating to Anaconda Navigator 1.10.0 (MacOS)
- Binary numbers in Python
- Django: ImproperlyConfigured: The SECRET_KEY setting must not be empty
- builtins.TypeError: must be str, not bytes
- How do you read from stdin?
- takes 1 positional argument but 2 were given
- What is dtype(‘O’), in pandas?
- Call a function from another file?
- What’s the difference between %s and %d in Python string formatting?
- What is the difference between Jupyter Notebook and JupyterLab?
- Python 3 ImportError: No module named ‘ConfigParser’
- Group by index + column in pandas
- Why am I getting a FileNotFoundError?
- Anaconda Navigator won’t launch (windows 10)
- What are all possible pos tags of NLTK?
- Can’t open Jupyter notebook with Anaconda
- How to change dataframe column names in pyspark?
- Pickle , read in data , unsupported pickle protocol: 3 python 2.7
- pygame clock.tick() vs framerate in game main loop
- How to troubleshoot an “AttributeError: __exit__” in multiproccesing in Python?
- TypeError : Unhashable type
- Read PDF in Python and convert to text in PDF
- How to read a file without newlines?
- Encrypt & Decrypt using PyCrypto AES 256
- Error: Segmentation fault (core dumped)
- can’t open tensorboard 0.0.0.0:6006 or localhost:6006
- How to add a new row to an empty numpy array
- Logical indexing with lists
- How to set timeout on python’s socket recv method?
- How does str(list) work?
- Automatic indentation for Python in Notepad++
- pandas groupby sort within groups
- Is there a head and tail method for Numpy array?
- What should I use to open a url instead of urlopen in urllib3
- How to find out whether a file is at its `eof`?
- TypeError: Image data can not convert to float
- coercing to Unicode: need string or buffer, NoneType found when rendering in django admin
- Convert from ASCII string encoded in Hex to plain ASCII?
- Does python support character type?
- Styling multi-line conditions in ‘if’ statements?
- What is related_name used for?
- Symbol not found: __PyCodecInfo_GetIncrementalDecoder
- How can I tail a log file in Python?
- How to write unicode strings into a file?
- python-How to set global variables in Flask?
- How do I install pip on macOS or OS X?