簡體   English   中英

如何從相同的python腳本運行.py和.exe文件

[英]how to run both a .py and a .exe file from the same python script

我想知道如何從相同的python腳本運行.py和.exe文件。

我有一個用於運行.py的腳本和一個用於運行.exe的腳本。

我的問題是我不知道如何從同一腳本運行它們! 我希望exe文件出現在兩個.py文件之間。

.py腳本

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import division  # so that 1/3=0.333 instead of 1/3=0
from psychopy import visual, core, data, event, logging, sound, gui
from psychopy.constants import *  # things like STARTED, FINISHED
import numpy as np  # whole numpy lib is available, prepend 'np.'
from numpy import sin, cos, tan, log, log10, pi, average, sqrt, std, deg2rad, rad2deg, linspace, asarray
from numpy.random import random, randint, normal, shuffle
import os  # handy system and path functions

"""find and execute all .py files in the current directory"""

import glob, sys
import subprocess
import random


def launch(exp):
    """launch a compiled PsychoPy experiment (.py) via subprocess"""
    command = [sys.executable, '-u', exp] # spaces in file names are ok
    proc = subprocess.Popen(command)
    proc.communicate()
    del proc


expts = ['myPythonFile1.py', 'myPythonFile2.py']
for exp in expts:
    launch(exp)

.exe腳本

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import division  # so that 1/3=0.333 instead of 1/3=0
from psychopy import visual, core, data, event, logging, sound, gui
from psychopy.constants import *  # things like STARTED, FINISHED
import numpy as np  # whole numpy lib is available, prepend 'np.'
from numpy import sin, cos, tan, log, log10, pi, average, sqrt, std, deg2rad, rad2deg, linspace, asarray
from numpy.random import random, randint, normal, shuffle
import os  # handy system and path functions

import subprocess

a=subprocess.Popen(r"myEXEfile.exe")
a1=os.system(r"myEXEfile.exe")

只需檢查每種類型。

def launch(exp):
    # check if exe or py
    if (exp.endswith(".exe")):                  # So it's an exe.
        a=subprocess.Popen(exp)
        a1=os.system(exp)
    else:                                       # Otherwise is a .py        
       # Launch a compiled PsychoPy experiment (.py) via subprocess.
        command = [sys.executable, '-u', exp] # spaces in file names are ok
        proc = subprocess.Popen(command)
        proc.communicate()
        del proc 

# And just put your exe filename between the two py files. 
expts = ['myPythonFile1.py', r'myEXEfile.exe', 'myPythonFile2.py']
for exp in expts:
    launch(exp)

此外,如果您不使用進程輸出,建議您使用proc.wait()而不是proc.communicate

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM