How to 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.

Dec 7, 2020 in Python by kartik
• 37,520 points
1,588 views

2 answers to this question.

0 votes

Hello @kartik,

You could also add the subdirectory to your Python path so that it imports as a normal script.

import sys
sys.path.insert(0, <path to dirFoo>)
import Bar

Hope it helps!!

Thank You!!

answered Dec 7, 2020 by Niroj
• 82,880 points
0 votes
If i understand your question correct then its actually pretty easy
Just to give you a example from my own program where i split things up

In my situation i have a folder within my project called includes where all my functions are stored within my main script
I have then added a normal import string like "from includes.menu import bin_menu" in main.py, in that situation import my function/module bin_menu from the file menu.py within the folder includes

The important thing is you need a empty .py file in the folder ( in includes in my example ) called __init__.py for python to be able to initiate the folder
answered Dec 14, 2020 by Tanja84DK

edited Dec 15, 2020

Related Questions In Python

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,890 points
2,470 views
0 votes
1 answer

Import a module from a relative path?

Assuming that both your directories are real ...READ MORE

answered Nov 25, 2020 in Python by Gitika
• 65,890 points
1,493 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,890 points
2,558 views
0 votes
1 answer
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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,364 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
3,935 views
0 votes
1 answer

How to remove a key from a Python dictionary?

Hello, If you need to remove a lot ...READ MORE

answered Apr 15, 2020 in Python by Niroj
• 82,880 points
1,649 views
0 votes
1 answer

How to delete items from a dictionary while iterating over it?

Hii Kartik, You could do it in two ...READ MORE

answered Apr 30, 2020 in Python by Niroj
• 82,880 points
2,841 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP