简体   繁体   English

从python中的另一个脚本调用时使用子进程时出错

[英]error using subprocess when calling from another script in python

I'm trying to use subprocess to call another script, but being a beginner I'm not quite getting there. 我正在尝试使用子流程来调用另一个脚本,但是作为一个初学者,我不太了解。

The first script defines the variables, and it will be saved in differing locations so it can't have an absolute path. 第一个脚本定义了变量,并且将其保存在不同的位置,因此它没有绝对路径。

The second script calls in the variables from the first script and will use them to do its thing. 第二个脚本从第一个脚本中调用变量,并将使用它们来完成任务。 This script will stay in the same directory. 该脚本将保留在同一目录中。

This is what I have so far 这就是我到目前为止

script1.py: script1.py:

import os
import sys
import subprocess

x = 'VALUE'

import subprocess
subprocess.call(["python", "/mnt/scratch/script2.py"])

script2.py: script2.py:

import sys
import subprocess

from script1 import x

Seems pretty basic but the error coming up is: ImportError: No module named x 似乎很基本,但是出现的错误是:ImportError:没有名为x的模块

Cheers 干杯

if your script 1.py is in some folder suppose D:\\Users\\Desktop\\Test While your Script2.py is in some other folder 如果您的脚本1.py在某个文件夹中,则假定D:\\ Users \\ Desktop \\ Test而您的Script2.py在其他文件夹中

provide path to the folder location of file 1 in script2.py with sys.path and that should work 在sys.path中提供script2.py中文件1的文件夹位置的路径,该路径应该可以工作

import sys
import subprocess
sys.path.append('D:\Users\Desktop\Test')
from script1 import x
print x 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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