Python Forum
package script cant find sibling script when executed from outside
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
package script cant find sibling script when executed from outside
#1
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?
Reply
#2
Please post the error message and the steps required to produce. Also post information about rhe directory structure.
Reply
#3
(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.
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  run part of a script with a different version of Python jdog 3 620 May-27-2024, 01:57 AM
Last Post: Alice12
Shocked Script get's really weird error DaJohn 7 307 May-26-2024, 09:10 AM
Last Post: snippsat
Music Python Script Repeating Number When Saving Facebook Photos ThuanyPK 2 217 May-13-2024, 10:59 PM
Last Post: ebn852_pan
  Trying to generating multiple json files using python script dzgn989 4 316 May-10-2024, 03:09 PM
Last Post: deanhystad
  Problems passing arguments containing spaces to bash script and then on to python kaustin 6 604 Apr-03-2024, 08:26 PM
Last Post: deanhystad
  Using a script to open multiple shells? SuchUmami 9 724 Apr-01-2024, 10:04 AM
Last Post: Gribouillis
  How to include one script into another? MorningWave 8 675 Mar-21-2024, 10:34 PM
Last Post: MorningWave
  ChromeDriver breaking Python script genericusername12414 1 411 Mar-14-2024, 09:39 AM
Last Post: snippsat
  using PowerShell from Python script for mounting shares tester_V 8 693 Mar-12-2024, 06:26 PM
Last Post: tester_V
  No Internet connection when running a Python script basil_555 8 852 Mar-11-2024, 11:02 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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