Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import a module from a relative path

How do I import a Python module given its relative path?

For example, if dirFoo contains Foo.py and dirBar, and dirBar contains Bar.py, how do I import Bar.py into Foo.py?

Here's a visual representation:

dirFoo\     Foo.py     dirBar\         Bar.py 

Foo wishes to include Bar, but restructuring the folder hierarchy is not an option.

like image 661
Jude Allred Avatar asked Nov 10 '08 21:11

Jude Allred


People also ask

How do you use relative import in Python?

Relative imports use dot(.) notation to specify a location. Single dot specifies that the module is in the current directory, two dots indicate that module is in its parent directory of the current location and three dots indicate that it is in grandparent directory and so on.

How do I import a Python path?

append() Function. This is the easiest way to import a Python module by adding the module path to the path variable. The path variable contains the directories Python interpreter looks in for finding modules that were imported in the source files.

What is relative path in Python?

A relative file path is interpreted from the perspective your current working directory. If you use a relative file path from the wrong directory, then the path will refer to a different file than you intend, or it will refer to no file at all.


2 Answers

Be sure that dirBar has the __init__.py file -- this makes a directory into a Python package.

like image 21
S.Lott Avatar answered Oct 09 '22 08:10

S.Lott


Assuming that both your directories are real Python packages (do have the __init__.py file inside them), here is a safe solution for inclusion of modules relatively to the location of the script.

I assume that you want to do this, because you need to include a set of modules with your script. I use this in production in several products and works in many special scenarios like: scripts called from another directory or executed with python execute instead of opening a new interpreter.

 import os, sys, inspect  # realpath() will make your script run, even if you symlink it :)  cmd_folder = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))  if cmd_folder not in sys.path:      sys.path.insert(0, cmd_folder)   # Use this if you want to include modules from a subfolder  cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"subfolder")))  if cmd_subfolder not in sys.path:      sys.path.insert(0, cmd_subfolder)   # Info:  # cmd_folder = os.path.dirname(os.path.abspath(__file__)) # DO NOT USE __file__ !!!  # __file__ fails if the script is called in different ways on Windows.  # __file__ fails if someone does os.chdir() before.  # sys.argv[0] also fails, because it doesn't not always contains the path. 

As a bonus, this approach does let you force Python to use your module instead of the ones installed on the system.

Warning! I don't really know what is happening when current module is inside an egg file. It probably fails too.

like image 111
sorin Avatar answered Oct 09 '22 09:10

sorin