0

I have 2 Python 3.5 scripts, boot.py and about.py. Boot.py is located in /os, but about.py is located in /os/bin. Boot.py starts about.py with os.system(/path/about.py). The problem is that about.py requires variables that are in boot.py, and i dont want to rewrite them all over again. So i need to start about.py in a way that it can read/use the variables in boot.py. If its unclear, i posted the codes down below.

boot.py:

#Boot.py
import os
import subprocess
import socket
import platform
import multiprocessing
import time
import datetime
from random import randint
#Create functions
def cls():
    os.system('cls' if os.name=='nt' else 'clear')
#Set the variables
prj_name = 'boot.py'
prj_build = 1.01
prj_developer = 'RED'
#Bunch of print() and input() commands below

about.py:

#Somehow get the variables and functions defined in boot.py
print('This project is made by ' + prj_developer)
print('Build: ' + prj_build)
print('Launched by: ' + prj_name)

1 Answer 1

1

Simply import the other file. By doing so the file will be run and the variables defined.

from boot import *
print('This project is made by ' + prj_developer)
print('Build: ' + prj_build)
print('Launched by: ' + prj_name)

I would also recommend putting all code of the other file, that shouldn't run when imported in a if statement (not necessary in this case though):

if __name__ == "__main__":
    pass # only run if file is executed directly (not from importing)

If the boot.py file is the directory above you would write (add a . for every parent directory):

from .. import boot.*

If the boot.py file is the directory above you would write (make sure to put a empty file called __init__.py in any subdirectories you are importing from):

from DIRECTORY.boot import *
Sign up to request clarification or add additional context in comments.

8 Comments

But the about.py and boot.py are located in different directories.
Then you need to use relative imports. see stackoverflow.com/questions/72852/…
I didn't understand how do i apply it in my code :(
I cant put all the files together, in fact, im making a OS emulator, in which boot.py calls about.py using os.system(). The about.py and all the other files are located in a subdirectory /bin.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.