How to import a module given the full path?


How can I load a Python module given its full path? Note that the file can be anywhere in the filesystem, as it is a configuration option.

2/16/2014 3:36:02 PM

Accepted Answer

For Python 3.5+ use:

import importlib.util
spec = importlib.util.spec_from_file_location("", "/path/to/")
foo = importlib.util.module_from_spec(spec)

For Python 3.3 and 3.4 use:

from importlib.machinery import SourceFileLoader

foo = SourceFileLoader("", "/path/to/").load_module()

(Although this has been deprecated in Python 3.4.)

For Python 2 use:

import imp

foo = imp.load_source('', '/path/to/')

There are equivalent convenience functions for compiled Python files and DLLs.

See also

5/9/2019 12:06:42 PM

The advantage of adding a path to sys.path (over using imp) is that it simplifies things when importing more than one module from a single package. For example:

import sys
# the mock-0.3.1 dir contains, &

from testcase import TestCase
from testutils import RunTests
from mock import Mock, sentinel, patch

If your top-level module is not a file but is packaged as a directory with, then the accepted solution almost works, but not quite. In Python 3.5+ the following code is needed (note the added line that begins with 'sys.modules'):

MODULE_PATH = "/path/to/your/module/"
MODULE_NAME = "mymodule"
import importlib
import sys
spec = importlib.util.spec_from_file_location(MODULE_NAME, MODULE_PATH)
module = importlib.util.module_from_spec(spec)
sys.modules[] = module 

Without this line, when exec_module is executed, it tries to bind relative imports in your top level to the top level module name -- in this case "mymodule". But "mymodule" isn't loaded yet so you'll get the error "SystemError: Parent module 'mymodule' not loaded, cannot perform relative import". So you need to bind the name before you load it. The reason for this is the fundamental invariant of the relative import system: "The invariant holding is that if you have sys.modules['spam'] and sys.modules[''] (as you would after the above import), the latter must appear as the foo attribute of the former" as discussed here.


To import your module, you need to add its directory to the environment variable, either temporarily or permanently.


import sys
import my_module


Adding the following line to your .bashrc file (in linux) and excecute source ~/.bashrc in the terminal:

export PYTHONPATH="${PYTHONPATH}:/path/to/my/modules/"

Credit/Source: saarrrr, another stackexchange question


It sounds like you don't want to specifically import the configuration file (which has a whole lot of side effects and additional complications involved), you just want to run it, and be able to access the resulting namespace. The standard library provides an API specifically for that in the form of runpy.run_path:

from runpy import run_path
settings = run_path("/path/to/")

That interface is available in Python 2.7 and Python 3.2+


You can also do something like this and add the directory that the configuration file is sitting in to the Python load path, and then just do a normal import, assuming you know the name of the file in advance, in this case "config".

Messy, but it works.

configfile = '~/'

import os
import sys


import config

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]