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

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:
|-- gui/
| |--
|-- custom_functions/
| |--
| |--
| |-- uses a function in (my_function), which in turn imports a function from (parseVariable).
While developping module2, I imported like this
from module1 import parseVariable
This works fine in module2, but would cause import errors in
So what I did in is to add the folder "custom_functions" to my path.
scriptLoc = os.path.dirname(os.path.realpath(__file__))
# add parent folder to path
# 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, 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?


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?
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?

Possibly Related Threads…
Thread Author Replies Views Last Post
  threading across imports Nickd12 2 294 Nov-09-2020, 01:59 AM
Last Post: Nickd12
  Absolute beginner am I missing something? kamren 7 477 Sep-25-2020, 05:32 AM
Last Post: buran
  chkFile with absolute paths JarredAwesome 7 399 Sep-21-2020, 03:51 AM
Last Post: bowlofred
  refreshing imports seandepagnier 4 512 Sep-20-2020, 11:51 PM
Last Post: seandepagnier
  Multimode imports fine as script but not after compiling into exe chesschaser 0 296 Aug-13-2020, 01:28 PM
Last Post: chesschaser
  Folders in folders jenost 5 634 Apr-26-2020, 05:35 PM
Last Post: jenost
  I am an absolute newbie.Help me! feynarun 4 564 Apr-11-2020, 09:25 PM
Last Post: feynarun
  sub-folders in folders from text line jenost 1 404 Mar-31-2020, 07:16 AM
Last Post: ndc85430
  Understanding the concept ( Modules , imports ) erfanakbari1 1 499 Nov-25-2019, 01:59 AM
Last Post: Larz60+
  Absolute newbie asking for help with blinking LEDs misterfrickel 0 650 Mar-15-2019, 08:54 AM
Last Post: misterfrickel

Forum Jump:

User Panel Messages

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