首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在包含扩充的python文件中运行python命令并读取输出

在包含扩充的python文件中运行python命令并读取输出
EN

Stack Overflow用户
提问于 2020-05-08 14:44:59
回答 1查看 119关注 0票数 0

我是python的新手,正在尝试将此命令的输出转换为字符串。

代码语言:javascript
复制
C:\GPT2\gpt-2-finetuning\src\generate_unconditional_samples.py --temperature 0.8 --top_k 40 --model_name run1-- nsamples  1 --length 100

下面是我尝试使用'>>>‘来表示每个问题的方法。

有没有我不知道的更好的方法?

python 3.7 spyder

How to run python file in Tkinter

代码语言:javascript
复制
from subprocess import Popen
p = Popen("C:\GPT2\gpt-2-finetuning\src\generate_unconditional_samples.py --temperature 0.8 --top_k 40 --model_name run1--nsamples  1 --length 100")
stdout, stderr = p.communicate()

>>>OSError: [WinError 193] %1 is not a valid Win32 application

Run a Python script from another Python script, passing in arguments

代码语言:javascript
复制
import os
os.system("C:\GPT2\gpt-2-finetuning\src\generate_unconditional_samples.py --temperature 0.8 --top_k 40 --model_name run1 --nsamples  1 --length 100")

>>>(Script does not complete and output is not gathered)

Assign output of os.system to a variable and prevent it from being displayed on the screen

代码语言:javascript
复制
text = os.popen('cat C:\GPT2\gpt-2-finetuning\src\generate_unconditional_samples.py --temperature 0.8 --top_k 40 --model_name run1 --nsamples  1 --length 100').read()

>>>(doesnt run it)

How to import a module given the full path?

代码语言:javascript
复制
import importlib.util
spec = importlib.util.spec_from_file_location("generate_unconditional_samples", "C:\GPT2\gpt-2-finetuning\src\generate_unconditional_samples.py")
foo = importlib.util.module_from_spec(spec)
spec.loader.exec_module(foo)
foo.MyClass()

>>>ModuleNotFoundError: No module named 'fire'(fire is the first import call in the module also no augments)

Python: read output while executing cmd commands

代码语言:javascript
复制
command='cmd /C C:\GPT2\gpt-2-finetuning\src\generate_unconditional_samples.py --temperature 0.8 --top_k 40 --model_name run1 --nsamples  1 --length 100'
proc=Popen(command,creationflags=CREATE_NEW_CONSOLE,stdout=PIPE)
output=proc.communicate()[0]
print (output)

>>>b''(thats the full output)

预期执行

代码语言:javascript
复制
C:\GPT2\gpt-2-finetuning>C:\GPT2\gpt-2-finetuning\src\generate_unconditional_samples.py --temperature 0.8 --top_k 40 --model_name run1 --nsamples  1 --length 100
2020-05-07 23:14:59.846175: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'cudart64_100.dll'; dlerror: cudart64_100.dll not found
2020-05-07 23:14:59.852383: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
WARNING:tensorflow:From C:\GPT2\gpt-2-finetuning\src\model.py:147: The name tf.AUTO_REUSE is deprecated. Please use tf.compat.v1.AUTO_REUSE instead.

WARNING:tensorflow:From C:\GPT2\gpt-2-finetuning\src\generate_unconditional_samples.py:52: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.

2020-05-07 23:15:02.748274: W tensorflow/stream_executor/platform/default/dso_loader.cc:55] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2020-05-07 23:15:02.754792: E tensorflow/stream_executor/cuda/cuda_driver.cc:318] failed call to cuInit: UNKNOWN ERROR (303)
2020-05-07 23:15:02.769396: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: DESKTOP-[redacted]
2020-05-07 23:15:02.775857: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: DESKTOP-6H0CO8Q
2020-05-07 23:15:02.782411: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
WARNING:tensorflow:From C:\GPT2\gpt-2-finetuning\src\generate_unconditional_samples.py:54: The name tf.set_random_seed is deprecated. Please use tf.compat.v1.set_random_seed instead.

WARNING:tensorflow:From C:\GPT2\gpt-2-finetuning\src\model.py:148: The name tf.variable_scope is deprecated. Please use tf.compat.v1.variable_scope instead.

WARNING:tensorflow:From C:\GPT2\gpt-2-finetuning\src\model.py:152: The name tf.get_variable is deprecated. Please use tf.compat.v1.get_variable instead.

WARNING:tensorflow:From C:\GPT2\gpt-2-finetuning\src\model.py:36: The name tf.rsqrt is deprecated. Please use tf.math.rsqrt instead.

WARNING:tensorflow:From C:\GPT2\gpt-2-finetuning\src\model.py:166: The name tf.add_to_collection is deprecated. Please use tf.compat.v1.add_to_collection instead.

WARNING:tensorflow:From C:\GPT2\gpt-2-finetuning\src\sample.py:65: to_float (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.cast` instead.
WARNING:tensorflow:From C:\GPT2\gpt-2-finetuning\src\sample.py:16: where (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
WARNING:tensorflow:From C:\GPT2\gpt-2-finetuning\src\sample.py:70: multinomial (from tensorflow.python.ops.random_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.random.categorical` instead.
WARNING:tensorflow:From C:\GPT2\gpt-2-finetuning\src\generate_unconditional_samples.py:63: The name tf.train.Saver is deprecated. Please use tf.compat.v1.train.Saver instead.

======================================== SAMPLE 1 ========================================
<user>dont be like that
<user>I dont want to be a kid anymore
(ect.)
EN

回答 1

Stack Overflow用户

发布于 2020-05-08 14:58:19

也许可以尝试调用python可执行文件(确保路径中有"python.exe“或指定完整路径):

代码语言:javascript
复制
from subprocess import Popen
p = Popen("python C:\GPT2\gpt-2-finetuning\src\generate_unconditional_samples.py --temperature 0.8 --top_k 40 --model_name run1 --nsamples 1 --length 100")
stdout, stderr = p.communicate()

参数中也有一些拼写错误( run1后面没有空格,--nsamples后面有两个空格)。你可以随时向Popen传递一个参数列表,这样就不必管理它了:

代码语言:javascript
复制
from subprocess import Popen
p = Popen(["python", "C:\GPT2\gpt-2-finetuning\src\generate_unconditional_samples.py", "--temperature",  "0.8", "--top_k", "40", "--model_name", "run1", "--nsamples", "1", "--length", "100"])
stdout, stderr = p.communicate()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61673537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档