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


Messages In This Thread
RE: package script cant find sibling script when executed from outside - by snippsat - Mar-03-2023, 04:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems passing arguments containing spaces to bash script and then on to python kaustin 6 478 Apr-03-2024, 08:26 PM
Last Post: deanhystad
  Using a script to open multiple shells? SuchUmami 9 631 Apr-01-2024, 10:04 AM
Last Post: Gribouillis
  How to include one script into another? MorningWave 8 577 Mar-21-2024, 10:34 PM
Last Post: MorningWave
  ChromeDriver breaking Python script genericusername12414 1 355 Mar-14-2024, 09:39 AM
Last Post: snippsat
  using PowerShell from Python script for mounting shares tester_V 8 589 Mar-12-2024, 06:26 PM
Last Post: tester_V
  No Internet connection when running a Python script basil_555 8 715 Mar-11-2024, 11:02 AM
Last Post: snippsat
Question Running Python script through Task Scheduler? Winfried 8 591 Mar-10-2024, 07:24 PM
Last Post: Winfried
  Combine console script + GUI (tkinter) dejot 2 467 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 384 Feb-17-2024, 12:29 PM
Last Post: deanhystad
  OBS Script Troubleshooting Jotatochips 0 318 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