Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在另一个python脚本中使用来自1个python脚本的输入_Python_Python 3.x - Fatal编程技术网

如何在另一个python脚本中使用来自1个python脚本的输入

如何在另一个python脚本中使用来自1个python脚本的输入,python,python-3.x,Python,Python 3.x,我有3个python脚本文件,比如,R.py,N.py,p.py。我使用P运行N,通过调用R的函数从R获取数据。我想在R.py中的ifstation中使用P中的输入。我知道我可以更改R.py以接收arg,但我必须大量更改R.py,因为if不在我调用的函数中。我是否有办法从P.py捕获输入,并在R的中使用它 p.py: import N.py as n lang = input("Enter the language set?: ) n.agent(name=name, learning=lr,

我有3个python脚本文件,比如,
R.py
N.py
p.py
。我使用
P
运行
N
,通过调用
R的
函数从
R
获取数据。我想在
R.py
中的
if
station中使用
P
中的输入。我知道我可以更改
R.py
以接收
arg
,但我必须大量更改
R.py
,因为
if
不在我调用的函数中。我是否有办法从
P.py
捕获
输入
,并在
R的
中使用它

p.py

import N.py as n

lang = input("Enter the language set?: )
n.agent(name=name, learning=lr, is_remote=is_remote, is_bi=is_bi, batch=batch, 
        no_char_codes=no_char_codes, classes=classes, data_set=lang)
import R as r

r.retrieve(start, batch_counter, is_remote)
N.py

import N.py as n

lang = input("Enter the language set?: )
n.agent(name=name, learning=lr, is_remote=is_remote, is_bi=is_bi, batch=batch, 
        no_char_codes=no_char_codes, classes=classes, data_set=lang)
import R as r

r.retrieve(start, batch_counter, is_remote)
R.py
(不完整):


通过合并python的读写函数,我可以看到这个问题的答案。制作一个所有3.py文件都可以查看的文本文档,例如,如果p.py提供“hello”输入(如果您想使用字符串文字作为输入)。您可以让p.py程序通过以下方式打开共享文本文档:

#(from p.py file)
input = "hello"
f = open("C:/Users/Directory/shared_file.txt", "w")
f.write(input)
f.close()

#(Then from r.py file)
f = open("C:/Users/Directory/shared_file.txt", "r")
input = f.readline() #or f.read() if its only 1 line with no '\n' or '\t' spacing
f.close()
input = str(input) #just to insure the input read is string for this example
if input == "Hi":
    print("True")
elif input == "Hello":
    print("second True")
else:
    print("False")

此脚本应打印“False”。这就是我让我的程序相互通信的方式。除此之外,我认为您可以拥有一个参数,该参数应该从一个.py返回另一个.py,但我不知道如何做到这一点!我希望这有助于您从不同的角度看待如何解决问题

假设我有3个文件:

mod1.py

condition = input()
import mod1 as m1
import mod2 as m2
mod2.py

condition = input()
import mod1 as m1
import mod2 as m2
mod3.py

condition = input()
import mod1 as m1
import mod2 as m2
如果我想在
mod3.py
内部使用
mod1.py
中的对象,我可以简单地将
mod3.py
中的模块链接到:

import mod2 as m2
if m2.m1.condition:
    print(m2.m1.condition)

我并不是说它漂亮或者我推荐它,这几乎肯定是个坏主意——但是,如果你一定要

p.py

import shelve

user_input = "Some user input"
with shelve.open('my_store') as holder:
    holder['input'] = user_input

import N
import shelve

user_input = None

with shelve.open('my_store') as holder:
    user_input = holder['input']

print(user_input)
R.py

import shelve

user_input = "Some user input"
with shelve.open('my_store') as holder:
    holder['input'] = user_input

import N
import shelve

user_input = None

with shelve.open('my_store') as holder:
    user_input = holder['input']

print(user_input)

想分享一下你现有代码的一个小例子吗?这将帮助我们提供适合您需求的解决方案。另请看一看。@jp_data_analysis:我添加了一些代码。人们利用这些私人时间回答问题,如果下面的答案中有一个回答了您的问题,请将其标记为这样,如果合适的话,可能会放弃投票。@SteveJ:不是我想要的答案。最后,我更改了R.py以使其正常工作。然后回答您自己的问题供其他人使用,请求跟进,或者如果您认为该问题对社区不有用,则删除该问题。我认为OP希望它反过来运行--input()将在mod3中。