简体   繁体   中英

Python: How to run a script from within a script?

I am starting to learn python and I have made a program that contains a login window. Once the user logs in, I want to run a new script with the actual program but I do not want to put that scripts code into the log in windows to keep code organized and neat. How would I go about doing that?

My code in a generic form is this:

from tkinter import *
from re import *
import tkinter.messagebox as box

window = Tk()
window.title( 'Please Login' )
frame = Frame( window )
entry = Entry( frame )
password = Entry( frame , show='*' )
lbl1 = Label( frame , text="Name: " )
lbl2 = Label( frame , text="Alpha Key: " )

pattern = \
compile( 'GENERICPASSWORD' )

def get_passwrd() :
    passwrd = ( password.get() )
    is_valid = pattern.match( passwrd )
    if is_valid :
        # This is where I would like it to run a new script with the actual program.
    else :
        box.showinfo( 'Access Denied' , 'Please Try Again' )

btn = Button( frame , text = 'Log In' , command=get_passwrd )
btn.grid( row=3 , columnspan=2 )
entry.grid( row=1 , column=1 )
password.grid( row=2 , column=1 )
lbl1.grid( row=1 , sticky=E )
lbl2.grid( row=2 , sticky=E )
frame.grid( row=1 , padx=20 , pady=20 )

Use the python package manager. If your other file was run_this.py and had a main function called main() you would add

import run_this
run_this.main()

to your script. (generally, the import statement will go at the top of your file, while the call to run_this.main() would go after the login logic.)

To understand a bit better, when you run a python script it adds the current directory to the python path . This path is where python searches for packages and modules. A module is a bit of code... a file with a .py ending. For my snippet to work, you would have to run your program from the directory containing run_this.py - so that that module would be on your path.

To allow you to better organize your code, you can also use packages- folders full of code. Any folder that conatins a file called __init__.py can be a package. Python will check each folder contained within a path folder to see if it is a package, and if so, will make its sub files available.

Say your structure was like this-

parent_folder
    gui.py
    sub_gui.py
    logic
        __init__.py
        run_this.py
        another_file.py

If you ran your application from parent_folder, you could use

import logic.run_this
import logic.another_file

in gui.py to access their code. Futher, from inside run_this.py, you could use

import gui
import logic.another_file

this is because gui gets picked up on the python path, while another_file doesn't... but it the package logic does get picked up, and that contains another_file.

You will notice that your python interpreter adds a couple things to your PYTHONPATH- inside your python directory are some folders like "site-packages" that get added to the path. Somewhere in there is a file or packaged named tkinter that you imported at the beginning of this file :)

EDIT

The actual contents of a module get executed when that module gets imported. Functions and classes get defined, and code that is not contained in anything gets executed. Code like

module A

a = "world"
print a

Module B

import A
print "hello"

would output "world hello". Instead, move your logic into a structure like a function-

module A

def main():
    a = "world"
    print a

Module B

import A
print "hello"
A.main()

would output "hello world". Note that main() is defined inside the module, so we refer to it as A.main! We could also import a single identifier from the module-

from A import main

would only import that function (and not any other functions or classes in the file). We could then execute it without referring to the module

main()

  • though in this case, we'd have to be careful not to have something else called main() in our current module! One final trick you will see in python projects is to add

    if name == 'main': main()

which would let you import the code without running it early, but still run that file as a standalone script.

I think you are looking for the subprocess module.

Here's some information about it: https://docs.python.org/2/library/subprocess.html

You can use it like this:

import subprocess
subprocess.call(["command", "arguments"])

As you said you started to learn python, you should look for:

import os
os.system('command')

But Using subprocess module is the better method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM