Import a module from a relative path

0 votes

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.

Nov 25, 2020 in Python by anonymous
• 10,420 points
271 views

1 answer to this question.

0 votes

Assuming that both your directories are real Python packages (do have the __init__.py file inside them), here is a safe solution for the inclusion of modules relative 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 the current module is inside an egg file. It probably fails too.

answered Nov 25, 2020 by Gitika
• 65,870 points

Related Questions In Python

0 votes
2 answers

How to Import a module from a relative path?

If i understand your question correct then ...READ MORE

answered Dec 14, 2020 in Python by Tanja84DK

edited Dec 14, 2020 81 views
0 votes
1 answer

Shortest path from source to and from a negative cycle using Bellman Ford in Python

class NegativeWeightFinder: def __init__(self, graph: nx.Graph): ...READ MORE

answered Nov 13, 2018 in Python by Nymeria
• 3,520 points
609 views
0 votes
0 answers

What is the difference between import module and from module import?

When both serves the same purpose, why ...READ MORE

May 22, 2019 in Python by Waseem
• 4,540 points
795 views
+1 vote
1 answer

Error:Unable to import path from django.urls

Hello @kartik, You need Django version 2 pip install ...READ MORE

answered Jul 2, 2020 in Python by Niroj
• 82,680 points
3,509 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,983 views
0 votes
0 answers
0 votes
1 answer

How to import a module given the full path?

For Python 3.5+ use: import importlib.util spec = importlib.util.spec_from_file_location("module.name", ...READ MORE

answered Nov 25, 2020 in Python by Gitika
• 65,870 points
106 views
0 votes
1 answer

How to access a module written in Python from C?

Hey, @Roshni: You can access a module written ...READ MORE

answered Jun 26, 2020 in Python by Gitika
• 65,870 points
276 views