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?
#1
This is not about the include command.
I am a PHP developer, just starting on python. I got stuck when I tried to save parts of the scripts into a separate .py file, just to avoid repetition, nothing more. Reading about module scopes, globals and parameters did not help to solve the include problem. Errors pop up one after another. This is why I ask this simple question:

Is there a simple way to put three .py scripts together?
So that the system would treat them as one .py?

Maybe using the os module, exiting to the operating system, creating a new .py script on the fly (by appending three .py's) and executing it?
Reply
#2
You can use the import system

Quick example

one.py
def func():
    return 'One.py'
two.py
def func():
    return 'Two.py'
three.py
def func():
    return 'Three.py'
main.py
import one, two, three

print(one.func())
print(two.func())
print(three.func())
Output:
One.py Two.py Three.py
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Thank you for the prompt reply. My case is more complicated.

two.py would have a function receiving a parameter which is a variable set in one.py

I tried a few import combinantion, wich failed. I think I will stay with one large script to avoid import.




(Mar-21-2024, 12:57 AM)menator01 Wrote: You can use the import system

Quick example

one.py
def func():
    return 'One.py'
two.py
def func():
    return 'Two.py'
three.py
def func():
    return 'Three.py'
main.py
import one, two, three

print(one.func())
print(two.func())
print(three.func())
Output:
One.py Two.py Three.py
Reply
#4
This sounds backward
Quote:two.py would have a function receiving a parameter which is a variable set in one.py

"functions" should not "receive" parameters from a module. functions should be passed arguments. If you want to call a function in module B using a variable defined in module A, I would write module A like this:
import B

parameter= 5
result = B.func(parameter)
You should post your code instead of having us guess the best way to organize the modules.
Reply
#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
#6
A stab in the dark without more info, but: If they are not too big and complicated, why don't you define the 3 scripts as functions in 1 Python file?

import everything needed

paths = .....
var = ....

def one():
    ....

def two():
    ...

def three():
    ...

if __name__ == "__main__":
    # do things
Reply
#7
A stab in the dark without more info, but: If they are not too big and complicated, why not try defining the 3 scripts as functions in 1 Python file?


import everything needed
 
paths = .....
var = ....
 
def one():
    ....
 
def two():
    ...
 
def three():
    ...
 
if __name__ == "__main__":
    # do things
Reply
#8
Thanks for your time, guys. Actually my code (which works perfectly without errors) does not handle the parameters. The openai chact model is using the functions when needed. I only wanted beautify the code by copying the functions into another .py and was looking for a solution outside of import command. If there is no such solution I'll keep it as it is. Thanks again.

(Mar-21-2024, 04:28 AM)deanhystad Wrote: This sounds backward

Quote:two.py would have a function receiving a parameter which is a variable set in one.py

"functions" should not "receive" parameters from a module. functions should be passed arguments. If you want to call a function in module B using a variable defined in module A, I would write module A like this:
import B

parameter= 5
result = B.func(parameter)
You should post your code instead of having us guess the best way to organize the modules.
Reply
#9
This is how it works now, I just tried to overdo it.
I'm backing out now, sorry, I'm only a few days into python.

(Mar-21-2024, 06:57 AM)Pedroski55 Wrote: A stab in the dark without more info, but: If they are not too big and complicated, why not try defining the 3 scripts as functions in 1 Python file?


import everything needed
 
paths = .....
var = ....
 
def one():
    ....
 
def two():
    ...
 
def three():
    ...
 
if __name__ == "__main__":
    # do things
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how include a python code in notpad++ plugin akbarza 2 653 Sep-25-2023, 08:25 PM
Last Post: deanhystad
  Regex Include and Exclude patterns in Same Expression starzar 2 803 May-23-2023, 09:12 AM
Last Post: Gribouillis
  How to include input as part of variable name Mark17 4 2,525 Oct-01-2021, 06:45 PM
Last Post: Mark17
  Can I include text using artist? tetrisbot 0 1,440 Aug-13-2020, 08:13 PM
Last Post: tetrisbot
  How to include pandas with pyinstaller Rickus 1 10,665 Feb-19-2020, 08:01 PM
Last Post: snippsat
  How to include Variable in File Path penahuse 3 7,319 Jan-05-2020, 03:08 AM
Last Post: ichabod801
  Cannot open include file: 'ft2build.h' thracian 0 4,831 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,649 Sep-20-2019, 03:49 PM
Last Post: Yoriz
  Trying to include an If statement in my While Loop junkankit 3 2,968 Jan-31-2019, 10:06 AM
Last Post: buran
  create dictionary from **kwargs that include tuple bluefrog 2 4,887 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