Python Forum
package script cant find sibling script when executed from outside - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: package script cant find sibling script when executed from outside (/thread-39531.html)



package script cant find sibling script when executed from outside - Bock - Mar-03-2023

Hello together,
i just finished my first own package but facing a problem when importing and executing things from outside:
I made a very minimal example as demonstration:

Folder "mymodule" contains:
#__init__.py
# empty

#constants.py
my_const = 42

#function.py
def my_func():
       print(my_const)
On the same level as mymodule is :
# test.py
from mymodule.function import my_func
my_func()
Unfortunately i get the error message: "no module named 'constant' " which confuses me since i can import constant in function.py properly.

Question: Why is that? ( Maybe this code should that never be necessary - let me know anything you have in mind :)

Thank you very much,
Bock

PS: how can i make a "tab" in the python brackets of the thread editor?


RE: package script cant find sibling script when executed from outside - deanhystad - Mar-03-2023

Please post the error message and the steps required to produce. Also post information about rhe directory structure.


RE: package script cant find sibling script when executed from outside - Gribouillis - Mar-03-2023

(Mar-03-2023, 11:12 AM)Bock Wrote: Unfortunately i get the error message: "no module named 'constant' "
According to your post, the module is named 'constants' and not 'constant'. Could this be the error?

You could try a relative import in function.py
from .constants import my_const
Note that 'relative' import means relative to the python packages hierarchy, not the file system's directories tree.


RE: package script cant find sibling script when executed from outside - snippsat - Mar-03-2023

As you see all files as you describe.
C:\Python310\mymodule
λ ls
__init__.py  constants.py  function.py  test.py 
my_func() will never work have to change to this.
#function.py
from mymodule.constants import my_const

def my_func():
    print(my_const)
Now can run test.py,and it will work.
C:\Python310\mymodule
λ python test.py
42
A little more about how a package works.
C:\Python310\mymodule
λ ptpython
>>> import mymodule
>>>
>>> dir(mymodule)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__'
As see here so is nothing added to namespace if just import like this.
I like to modify so can just to simple import like this and files are added.
In __init__.py
import mymodule.constants
import mymodule.function
Test again.
C:\Python310\mymodule
λ ptpython
>>> import mymodule
>>>
>>> dir(mymodule)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'constants', 'function', 'mymodule']

>>> mymodule.constants.my_const
42

>>> mymodule.function.my_func()
42
Or can import like this without modify __init__.py.
This will bring constants and function into the namespace of package.
C:\Python310\mymodule
λ ptpython
>>> from mymodule import constants, function
>>> constants.my_const
42
>>> function.my_func()
42

Some more examples you can look in link in this post.