Python Forum
How to include one script into another?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to include one script into another?
#5
(Mar-21-2024, 01:12 AM)MorningWave Wrote: two.py would have a function receiving a parameter which is a variable set in one.py
Can make a package then can have one import eg import my_pack then can access all from my_pack..
Depends on how your code are,this can tricky and not so easy when new to this.
Example.
myproject/
│
├── my_pack/                 
│   ├── __init__.py 
│   ├── egg.py  
│   ├── spam.py     
│   ├── foo.py
egg.py
# Absolute path when import from a other module inside a package 
from my_pack.spam import spam_var

def egg_func():
    return f'Variable: <{spam_var}> from spam.py'
spam.py
spam_var = 999
foo.py
def foo_func():
    return 'foo'
Here glue it togethere,this make import easy.
__init__.py
from .egg import egg_func
from .foo import foo_func
Usage of this package,see that i can access all from my_pack.
@snippsat ➜ /workspaces/codespaces-blank $ ptpython
>>> import my_pack
>>>
>>> my_pack.foo_func()
'foo'

>>> my_pack.egg_func()
'Variable: <999> from spam.py'

>>> my_pack.spam.spam_var
999
Reply


Messages In This Thread
RE: How to include one script into another? - by snippsat - Mar-21-2024, 06:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how include a python code in notpad++ plugin akbarza 2 737 Sep-25-2023, 08:25 PM
Last Post: deanhystad
  Regex Include and Exclude patterns in Same Expression starzar 2 911 May-23-2023, 09:12 AM
Last Post: Gribouillis
  How to include input as part of variable name Mark17 4 2,628 Oct-01-2021, 06:45 PM
Last Post: Mark17
  Can I include text using artist? tetrisbot 0 1,480 Aug-13-2020, 08:13 PM
Last Post: tetrisbot
  How to include pandas with pyinstaller Rickus 1 10,899 Feb-19-2020, 08:01 PM
Last Post: snippsat
  How to include Variable in File Path penahuse 3 7,433 Jan-05-2020, 03:08 AM
Last Post: ichabod801
  Cannot open include file: 'ft2build.h' thracian 0 4,898 Nov-11-2019, 09:08 PM
Last Post: thracian
  I need a query to select the numbers that include 2 digits of 3 omidvakili 1 1,703 Sep-20-2019, 03:49 PM
Last Post: Yoriz
  Trying to include an If statement in my While Loop junkankit 3 3,035 Jan-31-2019, 10:06 AM
Last Post: buran
  create dictionary from **kwargs that include tuple bluefrog 2 5,010 Oct-26-2016, 10:24 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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