Python Forum

Full Version: How to include one script into another?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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
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
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.
(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
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
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
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.
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