Python Forum
absolute imports between folders
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
absolute imports between folders
#1
Hello,

I'm working on a tkinter gui, and I keep having issues with imports.
I've spent quite some time reading about imports, but I guess it still hasn't sunk in.

My project folder is setup like so:
Project/
|-- gui/
| |-- my_gui.py
|-- custom_functions/
| |-- __init__.py
| |-- module1.py
| |-- module2.py

my_gui.py uses a function in module2.py (my_function), which in turn imports a function from module1.py (parseVariable).
While developping module2, I imported like this
from module1 import parseVariable
This works fine in module2, but would cause import errors in my_gui.py.
So what I did in my_gui.py is to add the folder "custom_functions" to my path.
scriptLoc = os.path.dirname(os.path.realpath(__file__))
# add parent folder to path
sys.path.append(str(Path(scriptLoc).parent))
# in this parent folder is a folder named custom_functions, add that too
sys.path.append(os.path.join(scriptLoc, 'custom_functions\\'))
from custom_functions.module2 import my_function
...And I converted the import in module2 to
from custom_functions.module1 import parseVariable
That works great, except when I now run module2 as my main script (if __name__ == '__main__')
Then it would cause an import error again.

So currently, when working/debugging in module2.py, i revert back to the original (from module1 import parseVariable) and after debugging I switch it back to from custom_functions.module1 import parseVariable

I'm sure there is a way that this import would work in both scenario's, but I can't figure it out Wall

Any help?

Thanks,
Mikis

sorry... it hit me that I should have typed

sys.path.append(os.path.join(str(Path(scriptLoc).parent), 'custom_functions\\'))[\python]

Now it works.
But I still feel this is not best practice, right?
[hr]
sorry... it hit me that I should have typed

[python]sys.path.append(os.path.join(str(Path(scriptLoc).parent), 'custom_functions\\'))
Now it works.
But I still feel this is not best practice, right?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Absolute paths in subprocess - file not found kittyticker 4 405 Jan-28-2024, 10:37 PM
Last Post: kittyticker
  Imports that work with Python 3.8 fail with 3.9 and 3.10 4slam 1 2,550 Mar-11-2022, 01:50 PM
Last Post: snippsat
  Imports in my first package cuppajoeman 1 1,912 Jun-28-2021, 09:06 AM
Last Post: snippsat
  script with imports works but pytest gives "ModuleNotFoundError"? Hpao 0 1,544 Jun-27-2021, 08:30 PM
Last Post: Hpao
  Help wanted with python imports petros21 3 2,481 Apr-07-2021, 07:16 PM
Last Post: snippsat
Question How to include Modules not found (conditional imports) in my setup.py when I want to cff 0 3,767 Mar-17-2021, 11:57 AM
Last Post: cff
  automatically get absolute paths oclmedyb 1 2,064 Mar-11-2021, 04:31 PM
Last Post: deanhystad
  threading across imports Nickd12 2 2,105 Nov-09-2020, 01:59 AM
Last Post: Nickd12
  Absolute beginner am I missing something? kamren 7 3,109 Sep-25-2020, 05:32 AM
Last Post: buran
  chkFile with absolute paths JarredAwesome 7 2,917 Sep-21-2020, 03:51 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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