Python Forum
import a function from another file using relative path
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
import a function from another file using relative path
#1
Hi

It's a naive question to find the correct synthax (all i tested failed):

Context: In MyFile1.py, i want to load the function1 and function2.

If all files are located in the current directory (traditional situation):
from MyFile1 import function1, function2
Now i move-up "MyFile1.py" into the upper directory:
from ..MyFile1 import function1, function2
Error:
ImportError: attempted relative import with no known parent package
Same error if i create a module:
MyModule.py
from MyFile1 import function1, function2
Then I call the module
from ..MyModule import function1, function2
What I'm missing (it supposed to be quite simple)?

Thanks
Reply
#2
If the directory is in the same level as the executing file

from directory.mymodule import function
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
No: the executing file is not in the same directory.
  • /MyFunctionsDirectory where MyFile1.py is
  • /MyFunctionsDirectory/test where my executing file is and in which i want to import function1 and function2
Reply
#4
Well after digging into internet, I've found the below solution for my case by adding:
import sys
sys.path.append('../')
I'll need to go further to figure out (not so obvious topic).
Reply
#5
That's because you are mistaking Python packages for directories. They are not the same thing, although directories are used to construct packages.

Relative imports are relative to Python packages, not to system directories.

If function1 is in MyPackage.MyLibrary, then in MyPackage.MyModule you can use the relative import
from .MyLibrary import function1
and in MyPackage.MySubpackage.MyModule, you can use the relative import
from ..MyLibrary import function1
The .. goes to the grandparent package, which has nothing to do with the grandparent directory.
« We can solve any problem by introducing an extra level of indirection »
Reply
#6
Top take a little on the basic of understanding how a package work,this how i like to set it up.
project_env/
└── project/
    ├── __init__.py
    ├── foo/       
    │   └── bar.py
    └── main.py
So project is the package folder,and this folder most be in sys.path this i where Python will look for it.
I always have one __init__.py under prospect folder with content to bind package together and lift sub modules.
This make it easier to use the package.

Use the package:
G:\div_code
λ ptpython
>>> import projects
>>>
>>> projects.bar.answer_to_life()
42

>>> projects.main.main_func()
'Now in main func'
Or like import like this,goal is to make import easy to use.
>>> from projects import bar, main
>>>
>>> main.main_func()
'Now in main func'

>>> bar.answer_to_life()
42
Files in package.
__init__.py:
from . import main
from .foo import bar
main.py
def main_func():
    return 'Now in main func'
bar.py
def answer_to_life():
    return 42

Look this Thread here talk about different ways how sys.path works,and add to it.
Reply
#7
Thanks all for the explanations. Let me having a look on it and going back if i've additional questions.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to write variable in a python file then import it in another python file? tatahuft 4 943 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  File path by adding various variables Mishal0488 2 3,867 Apr-28-2023, 07:17 PM
Last Post: deanhystad
Video doing data treatment on a file import-parsing a variable EmBeck87 15 5,670 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Import XML file directly into Excel spreadsheet demdej 0 1,581 Jan-24-2023, 02:48 PM
Last Post: demdej
  Script File Failure-Path Error? jerryf 13 7,641 Nov-30-2022, 09:58 AM
Last Post: jerryf
  Import a module one step back of the path prathampatel9 1 1,797 Sep-21-2022, 01:34 PM
Last Post: snippsat
  How from sklearn.datasets import load_diabetes change to import to CSV file Anldra12 0 2,695 Dec-25-2021, 07:20 PM
Last Post: Anldra12
  How to import file and function in another folder SriRajesh 1 5,470 Dec-18-2021, 08:35 AM
Last Post: Gribouillis
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 3,171 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  'import Path' do not understand how to use it tester_V 2 2,531 Jun-19-2021, 02:23 AM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020