Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Modules in Python
#1
Hello everyone,

i have the following problem with modules and my folder structure:

thats my folder and file structur:

main-folder
-python module 1
-sub-folder B
--python module 2
--python module 3

Module 1 requires importation of module 2 and module 2 needs module 3
so in module 1 i have:
[python]import B.module2[python]
and in module 2 i have:
[python]import module2[python]

But now i get the following error if i try to run module1:
No module named 'module3'

I hope this abstract explanation is clear enough.

Thanks
Ewkos
Reply
#2
(May-26-2017, 05:24 PM)Ewkos Wrote: so in module 1 i have:
import B.module2
and in module 2 i have:
import module2
But now i get the following error if i try to run module1:
No module named 'module3'

Neither of those examples are trying to import module 3.
Supposing that's a type-o, and module2 is trying to import module3, then try from . import module3 in module2.py.

ie, this works:
### /module1.py
import B.module2
print(B.module2.spam)



### /B/module2.py
from . import module3
spam = module3.foo



### /B/module3.py
foo = "baz"
The reasoning is that import searches sys.path, which includes the directory that the currently running script is in... but it doesn't include subdirectories. So module2 can't "see" module3, because the B directory isn't being searched by import. Doing from . import instead, tells import very specifically to search the current directory, regardless of where module2 is being called from.

This behavior is new to python3, and worked differently in python2.

This also only works if module2.py is being imported... doing this will not work if you want to run module2.py directly.
Reply
#3
Hi thanks for your help!

Yeah you predicted correctly, module 2 needs to import module 3, sry for this mistake.

With your hint "from . import module3" this error vanishs and my code works, thanks!

Is there another way to import module3 such that module2 can be used without being importet or do i have to make a copy of this file instead?
Reply
#4
You'd need to check if it's the current main file that's running, and import the external file normally. Something like:

### B/module2.py
if __name__ == "__main__":
    import module3
else:
    from . import module3

spam = module3.foo
Reply
#5
Ah ok yeah, thats a good solution, thanks!

I have another question:
I have the following folder-structure:

Main-Folder
-Folder A
--main.py
-Folder B
--test.py

Now i want to use some function of test.py in main.py.

Up to now i have the following code:
import sys
sys.path.append(".")
from B import test

Then i get the following error:
ImportError: No module named 'B'

Would be nice if you can me help with this problem too.
Reply
#6
"." is the current directory. If you want to modify sys.path, then use ".." to refer to the parent directory.
Reply
#7
Ah ok yeah thats true.
Thanks, now it works.

Now i got another problem:
in the importet module i want to read the following txt file (this file is also in folder B as test.py):
test.txt

if i use this:
fobj = open("spam.txt")
in the test-py module

But i get the error:
FileNotFoundError: [Errno 2] No such file or directory: 'spam.txt'

This is wondering me, because usually this works, if the module and the file is in the same folder or do i have to load this file as like i am in folder a where this module will be run?

Thank you!
Reply
#8
You are more into package territory now.
It a better way to structuring Python’s module and usually has a __init__.py in each folder.
Example.
main_foo/
 |-- __init__.py
 |-- foo.py
 sub/
   |-- __init__.py
   |-- bar_1.py
   |-- bar_2.py
# foo.py
def egg():
   print('i am foo')
# bar_1.py
def spam():
   return 'hello from bar_1'
# bar_2.py
from main_foo.sub.bar_1 import spam

def goo():
   print('i am bar__2 and hello from <{}>'.format(spam()))
Test:
>>> from main_foo.foo import egg

>>> egg()
i am foo

>>> from main_foo.sub import bar_1, bar_2
>>> bar_1.spam()
'hello from bar_1'

>>> bar_2.goo()
i am bar__2 and hello from <hello from bar_1>
To get cleaner import i almost always modify __init__.py under main_foo.
This way will import main_foo contain access to all files.
from main_foo.foo import egg
from .sub import bar_1
from .sub import bar_2
Test again:
>>> import main_foo

>>> main_foo.egg()
i am foo

>>> main_foo.bar_1.spam()
'hello from bar_1'

>>> main_foo.bar_2.goo()
i am bar__2 and hello from <hello from bar_1>
Reply
#9
Hi, thanks for this link!

I have a question to this init.py:
Do they have to be empty or what do they include? The given link sadly dont answers this question; its only sad that they have to exist. Also stackoverflow and other packes cant help me.

To my above question:
I have the following Folder and file structure:

Folder A
-FolderB
--spam.txt
--read.py
--readSpam.py
-FolderC
--foo.py

There readSpam.py has a function pick() which calls read to read the txt file.

readSpam calls read to read a txt-file in a given format, so with this module i can read spam.txt
If i want to run readspam.py i just have to use the code:
fobj = open("test.txt")
Then i can open the file and read it with read.py

But i want to do this in foo, so i include readSpam in foo and there i call:
result = readSpam.pick()

Now i get the error:
FileNotFoundError: [Errno 2] No such file or directory: 'spam.txt'

But if i put spam.txt in the same folder as foo.py the code works.
So my question is, how do i have to change my code to leave spam.txt in folder B?
Reply
#10
(May-28-2017, 11:56 AM)Ewkos Wrote: I have a question to this init.py:
In my first run both __init__.py is empty.
>>> from main_foo.foo import egg

>>> egg()
i am foo

>>> from main_foo.sub import bar_1, bar_2
>>> bar_1.spam()
'hello from bar_1'

>>> bar_2.goo()
i am bar__2 and hello from <hello from bar_1>
To get a cleaner import access to all in import main_foo
I modify __init__.py as show in post.
# from this import i get access to all files
>>> import main_foo

>>> main_foo.egg()
i am foo

>>> main_foo.bar_1.spam()
'hello from bar_1'

>>> main_foo.bar_2.goo()
i am bar__2 and hello from <hello from bar_1>
Try to run my setup in post all info is there,to better understand this.
It can be hard to understand this stuff  Confused
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to see the date of installation of python modules. newbieAuggie2019 4 1,569 Mar-31-2023, 12:40 PM
Last Post: newbieAuggie2019
  Python modules for accessing the configuration of relevant paths Imago 1 1,365 May-07-2022, 07:28 PM
Last Post: Larz60+
  Python modules to extract data from a graph? bigmit37 5 22,377 Apr-09-2021, 02:15 PM
Last Post: TysonL
  Where to add own python modules in WinPython? HinterhaeltigesSchlaengelchen 1 2,272 Jan-21-2021, 07:45 PM
Last Post: snippsat
  Including modules in Python using sys.path.append JoeDainton123 1 2,891 Aug-24-2020, 04:51 AM
Last Post: millpond
  how to get PID's of linux commands executed through python modules? Manikandan_PS 4 3,043 Mar-12-2020, 07:16 AM
Last Post: Manikandan_PS
  python modules davy_yg 1 1,976 Sep-25-2019, 03:14 AM
Last Post: metulburr
  Importing Custom Modules in Python 3 Flexico 1 2,574 Aug-24-2019, 08:11 PM
Last Post: snippsat
  Trouble importing modules on a new python version snackman_barry 2 2,560 Jul-12-2019, 11:15 AM
Last Post: snackman_barry
  Import Python Modules zowhair 4 2,796 Jun-27-2019, 06:30 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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