Skip to content
Advertisement

How can I import a module dynamically given the full path?

How do I load a Python module given its full path?

Note that the file can be anywhere in the filesystem where the user has access rights.


See also: How to import a module given its name as string?

Advertisement

Answer

For Python 3.5+ use (docs):

JavaScript

For Python 3.3 and 3.4 use:

JavaScript

(Although this has been deprecated in Python 3.4.)

For Python 2 use:

JavaScript

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

See also http://bugs.python.org/issue21436.

Advertisement