Let's have MyClass in module.name module defined at /path/to/file.py. Below is how we import MyClass from this module

For Python 3.5+ use (docs):

import importlib.util
import sys
spec = importlib.util.spec_from_file_location("module.name", "/path/to/file.py")
foo = importlib.util.module_from_spec(spec)
sys.modules["module.name"] = foo
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.

Answer from Sebastian Rittau on Stack Overflow
🌐
Python.org
discuss.python.org › ideas
Import files directly from file location - Ideas - Discussions on Python.org
July 25, 2023 - Importing new modules in Python is a pain, and far from straightforward. The trivial way to import a new module would be to give the full path of the file containing the required code - which could be a python __init__ m…
Discussions

python - Importing files from different folder - Stack Overflow
For most programmers coming here from a search engine, this is not the answer you are looking for. Typically you would structure your files into packages (see other answers) instead of modifying the search path. By default, you can't. When importing a file, Python only searches the directory ... More on stackoverflow.com
🌐 stackoverflow.com
How to import a file from one level above?
Is it a python file or are you trying to read/write something? More on reddit.com
🌐 r/learnpython
13
3
June 21, 2022
What is the best way to import a module from a path?
It's probably the responsibility of the script that boots your python script to set PYTHONPATH. From the perspective of python, make sure the environment is set up correctly before the interpreter boots up. In other words, don't do this! More on reddit.com
🌐 r/learnpython
28
66
November 14, 2019
Import from parent directory
There's no nice and simple solution because there is no reason you should want to do that. Whatever it is you are trying to do, there's a official and better solution to use. However, if you really want to do it anyway, there's an easy hack. # b_file.py import sys sys.path.append("..") from a import a_file (this is the same as modifying PYTHONPATH, just doing it from within the python program instead of externally) More on reddit.com
🌐 r/learnpython
7
2
September 5, 2022
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-import-a-python-module-given-the-full-path
How to import a Python module given the full path? - GeeksforGeeks
July 23, 2025 - The path variable contains the directories Python interpreter looks in for finding modules that were imported in the source files. ... In this example Python code adds the 'articles' directory to the system path, allowing the import of the 'gfg' module. It then calls the 'method_in' from the 'GFG' class and 'method_out' from the 'gfg' module.
🌐
Delft Stack
delftstack.com › home › howto › python › import python file from path
How to Import a Module From a Full File Path in Python | Delft Stack
March 11, 2025 - Whether you are a beginner or an experienced developer, understanding these techniques will enhance your Python programming skills and streamline your workflow. One of the most straightforward methods to import a module from a specific file path in Python is by using sys.path.append().
🌐
EyeHunts
tutorial.eyehunts.com › home › python import module from path | example code
Python import module from path | Example code
August 4, 2022 - The importlib.util is one of the modules included in this package that can be used to import the module from the given path. import importlib.util spec = importlib.util.spec_from_file_location("main", "modules/main.py") foo = importlib.util.module_from_spec(spec) spec.loader.exec_module(foo) print(foo.var)
🌐
Medium
medium.com › @muhsinmuhammed1819 › this-is-how-to-make-python-import-from-the-path-we-want-dfeb431101de
This is how to make python import from the path we want. | by Muhsin Muhammed | Medium
March 1, 2024 - """ This is the search.py file inside search_engine directory """ import os MAIN_FOLDER = "search_engine" main_folder_path = os.path.join(os.path.abspath(__file__).split(MAIN_FOLDER)[0],MAIN_FOLDER) Please note here that you have to give the name of your main folder explicitly. The built-in importlib.util provides more flexibility to python imports. Using this module we can provide import from the absolute path of the module.
Find elsewhere
🌐
Real Python
realpython.com › ref › glossary › import-path
import path | Python Glossary – Real Python
In Python, the import path refers to the list of locations (path entries) that Python’s path-based finder searches to find the modules you want to import. During an import, these locations usually come from the sys.path variable, which is initialized from the directory of the running script, the PYTHONPATH environment variable, and installation-dependent defaults (including site-packages).
🌐
Better Stack
betterstack.com › community › questions › importing-files-from-different-folders-python
Importing files from different folder in Python? | Better Stack Community
Alternatively, you can use the PYTHONPATH environment variable to add the path to the folder containing the file you want to import. You can also use the importlib.import_module() function to import modules from different folders by providing ...
🌐
Python for the Lab
pythonforthelab.com › blog › complete-guide-to-imports-in-python-absolute-relative-and-more
Complete Guide to Imports in Python: Absolute, Relative, and More | Python For The Lab
While importing, we are not specifying a path in the file system, but rather an import path. Therefore, the imports are always relative to the PYTHONPATH, even if called absolute. Another option for importing modules is to define the relative path. We can continue building on the example from the previous section.
🌐
Pythonhow
pythonhow.com › what › what-to-do-to-import-files-from-different-folder-in-python
What to do to import files from a different folder in python
You can do this by adding the path to module1 to the Python path and then using the import statement to import file1.py: import sys sys.path.append("module1") import file1
🌐
TutorialsPoint
tutorialspoint.com › How-to-import-a-Python-module-given-the-full-path
How to retrieve Python module path?
April 17, 2025 - C:\Python313\Lib\math.py C:\Python313\Lib\inspect.py · Sometimes you need just the directory path where the module is located. Use os.path.dirname() to extract the directory ? import os import sys module_file = sys.__file__ module_dir = os.path.dirname(module_file) print("Module file:", module_file) print("Module directory:", module_dir)
🌐
GitHub
gist.github.com › MRobertEvers › 55a989b4883ea8d7715d2e156627f034
Import Python Files From Another Directory Python 3 · GitHub
That means we will be able to import <directory> as a module. Notice that one of the directories in sys.path by default is the directory that the python file launched from. We can use that information to interpret the line When importing the package, Python searches through the directories on sys.path looking for the package subdirectory.
🌐
Sentry
sentry.io › sentry answers › python › import files from a different folder in python
Python Import From Parent Directory and Subfolders | Sentry
Import modules from parent, child, and sibling directories using package relative imports with __init__.py or by adding paths to sys.path at runtime
🌐
Python documentation
docs.python.org › 3 › reference › import.html
5. The import system — Python 3.15.1 documentation
When the import system searches a regular package’s __path__ and encounters a subdirectory that does not contain an __init__.py file, that subdirectory becomes a portion contributing to a namespace subpackage of the enclosing regular package. See also PEP 420 for the namespace package specification. To begin the search, Python needs the fully qualified name of the module (or package, but for the purposes of this discussion, the difference is immaterial) being imported. This name may come from various arguments to the import statement, or from the parameters to the importlib.import_module() or __import__() functions.
🌐
Jefftk
jefftk.com › p › importing-a-python-file-by-name
Importing a Python File by Name - Jeff Kaufman
January 25, 2024 - import "/path/to/2024-01-25--strand-split-artifacts.py" But this doesn't work: import wants a module name, not a filename. The simplest way I know to import a python file from a path is:
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-import-module-outside-directory
Python - Import module outside directory - GeeksforGeeks
July 12, 2025 - Using importlib.util, modules can be loaded without modifying sys.path or making the folder a package, making it ideal for standalone or customized setups. In this Example importlib.util is used to dynamically load and import a module from an external file path, allowing access to its functions like hello() without modifying system paths.
🌐
Medium
medium.com › @vamsik23 › import-local-python-modules-a6e74950de4a
Import Local Python Modules. We all use modules in most of our… | by vamsi | Medium
August 18, 2021 - Here we can see that I am adding folder_1 to python path using sys.path.append function so when I ask python to import some modules then it will check in folder_1 too. Please keep in mind to add whole path to the folder, don’t use “~” in the append path.
🌐
freeCodeCamp
freecodecamp.org › news › python-import-from-file-importing-local-files-in-python
Python Import from File – Importing Local Files in Python
July 6, 2023 - Once we have the correct file path, we can use various methods to import files into our Python program. Built-in functions: The open() function is commonly used for reading text files. Pandas library: Offers functions for loading and importing various file formats, such as CSV, Excel, JSON, and more. NumPy library: Provides methods for importing data from binary files.