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
  Problems passing arguments containing spaces to bash script and then on to python kaustin 6 415 Apr-03-2024, 08:26 PM
Last Post: deanhystad
  Using a script to open multiple shells? SuchUmami 9 542 Apr-01-2024, 10:04 AM
Last Post: Gribouillis
  How to include one script into another? MorningWave 8 497 Mar-21-2024, 10:34 PM
Last Post: MorningWave
  ChromeDriver breaking Python script genericusername12414 1 321 Mar-14-2024, 09:39 AM
Last Post: snippsat
  using PowerShell from Python script for mounting shares tester_V 8 548 Mar-12-2024, 06:26 PM
Last Post: tester_V
  No Internet connection when running a Python script basil_555 8 663 Mar-11-2024, 11:02 AM
Last Post: snippsat
Question Running Python script through Task Scheduler? Winfried 8 522 Mar-10-2024, 07:24 PM
Last Post: Winfried
  Combine console script + GUI (tkinter) dejot 2 434 Feb-27-2024, 04:38 PM
Last Post: deanhystad
  How to receive two passed cmdline parameters and access them inside a Python script? pstein 2 355 Feb-17-2024, 12:29 PM
Last Post: deanhystad
  OBS Script Troubleshooting Jotatochips 0 302 Feb-10-2024, 06:18 PM
Last Post: Jotatochips

Forum Jump:

User Panel Messages

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