Python os.system(python“program”)不起作用

时间:2017-09-13 17:12:39

标签: python python-2.7 subprocess libraries

我正在编写一个包含多段代码的程序,并且不希望有人必须从安装盘手动复制粘贴整个目录,所以我写了一些可以为它们做的事情(比如自动运行。)

然而,当我尝试将程序复制到用户的计算机后启动程序时,我得到:

Process finished with exit code 0

而不是安装程序启动程序。

这是我正在使用的def:

import os

get_project_base_directory()
    check_version()
    print jason_dir
    new_dir = str(jason_dir+"/Jason_Core/Boot.py")
    command = "python " + new_dir
    print command
    os.system(command)

为什么os.system可能无法正常工作?我也试过popopen和subprocess,但输出相同(退出代码0)。

编辑:我在脚本中运行它,而不是python命令行。

编辑2:根据要求,Boot.py:

# 3rd party libraries
import os
import subprocess
import shutil
from imp import find_module


# My code to import


basedir = ""

# Get the project base directory (drive located on)
def get_project_base_directory():
    global basedir
    cwd = os.getcwd()
    basedir = "/".join(cwd.split("\\")[:len(cwd.split("\\"))-1])
    #print basedir


# Copies libraries from disk drive
def copy_libs():
    new_dir = "c:/Downloads/Python_Libraries"
    if os.path.exists(new_dir):
        shutil.rmtree(new_dir)
    old_dir = basedir + "/Libraries/Installed"
    #print old_dir
    try:
        shutil.copytree(old_dir, new_dir)
    except:
        print "     Error copying directories! Try manual copy."
        print "     Go to:", basedir + "/Libraries/Installed"


# Checks to see if the library is installed
def check_lib_install():
    temp_var = True
    libs_list = ("fbchat", "gtts", "pyaudio", "googlevoice", "espeak", "pywapi", "speech_recognition", "win10toast") # names of libs
    for lib in libs_list:
        print "Checking lib installation:", lib
        try:
            __import__(lib)
            print "     Lib installed"
            print ""
        except:
            print "     Lib not installed"
            install_lib(lib, libs_list.index(lib))
    print "All libraires installed!"


# Install libraries if they don't exist on this machine.
def install_lib(lib, index):
    print "     Installing lib:", lib
    libs_file_list = ("/fbchat-1.0.19/", "/gTTS-1.2.2/", "/PyAudio-0.2.11/", "/pygooglevoice-master/", "/python-espeak-0.5/", "/pywapi-0.3.8/", "/speech_recognition-master/", "/Windows-10-Toast-Notifications-master/")  # path to file
    print "Installing:", lib
    new_dir = "c:/Downloads/Python_Libraries" + libs_file_list[index]
    os.chdir(new_dir)
    try:
        temp_command = "python setup.py install"
        subprocess.call(temp_command)
    except:
        print lib, "failed installation. Try manual install."
    print ""


# Gets version number of local copy of Jason, and then the version number of the one on the disk drive. If the local version is lower, the python files are overwritten. If it doesn't yet exist locally, Jason is installed.
def check_version():
    local_version = "0"
    jason_dir = "C:/Jason_AI" # Where is jason installed to?
    if os.path.exists(jason_dir):
        with open(jason_dir+"\System Files\Version Number.txt") as local_jason:  # Where is jason installed to?
            counter = 0
            for line in local_jason:
                counter += 1
                if counter == 2:
                    local_version = line
                    #print local_version
                elif counter == 3:
                    break
        install_version_file = basedir + "\System Files\Version Number.txt"
        install_version_file = "/".join(install_version_file.split("\\"))
        with open(install_version_file) as drive_jason:
            counter = 0
            for line in drive_jason:
                counter += 1
                if counter == 2:
                    install_version = line
                    #print install_version
                    if install_version == local_version:
                        print "Version up to date!"
                        print "Using version number:", local_version
                    else:
                        shutil.rmtree(jason_dir)
                        print "Version outdated"
                        print "Using version number:", local_version
                elif counter == 3:
                    break
    else:
        print "Version outdated"
        print "Using version number:", local_version


# Controls the boot process, when done, launches into Listen.get_audio()
def control():
    get_project_base_directory()
    copy_libs()
    check_lib_install()
    import System_Logins # Importing this here since the required libraries might not be installed if its the first time running Jason on this machine
    import Actions  # ^^
    import System_Commands # ^^
    import Listen # ^^
    check_version()
    logins = System_Logins.read_logins(basedir)
    System_Commands.clear_screen()
    while True:
        speech = Listen.get_audio()
        Actions.sort(speech, logins)


def check_packages():
    packages = ["System_Logins", "Actions", "System_Commands", "Listen"]
    for package in packages:
        try:
            __import__(package) #Need to work on
            print package + ":", "successfully loaded."
            packages[packages.index(package)] = "True"
        except ImportError:
            print package + ":", "error."
    return packages


#Remote login (ie, from install)
def remote_control():
    packages = check_packages()
    #print packages
    if any(word in ("System_Logins", "Actions", "System_Commands", "Listen") for word in packages):
        control()
    import System_Logins  # Importing this here since the required libraries might not be installed if its the first time running Jason on this machine
    import Actions  # ^^
    import System_Commands  # ^^
    import Listen  # ^^
    logins = System_Logins.read_logins(basedir)
    System_Commands.clear_screen()
    while True:
        speech = Listen.get_audio()
        Actions.sort(speech, logins)


remote_control()

0 个答案:

没有答案