简体   繁体   English

在 GNU Octave 中执行 Python 文件

[英]Execute a Python file in GNU Octave

I write a code to run a python file in GNU Octave.我编写了一个代码来在 GNU Octave 中运行 python 文件。 This is that code part.这就是代码部分。

[output,status]=python('customers.py');
output
status

But when execute that code I get an error like this.但是当执行该代码时,我会收到这样的错误。

'python' is not recognized as an internal or external command,
operable program or batch file.
output =
status =  1

I don't understand why I got an error like this.我不明白为什么会出现这样的错误。 The python file is also in the same directory. python 文件也在同一目录中。 This is the python code:这是 python 代码:

# Import sys module
import sys

# Total number of arguments
print('Total arguments:', len(sys.argv))

#print("Argument values are:")
# Iterate command-line arguments using for loop
#for i in sys.argv:
  #print(i)

# Define a dictionary
customers = {'9876':'Kamal Perera','9873':'Amal Fernando',
'9865':'Vimal Gunasena','9843':'Chamal Sirisena', '9862':'Dumal Zoysa',
'9831':'Nimal Walpola'}

#print("The customer names are:")
# Print the values of the dictionary
#for customer in customers:
    #print(customers[customer])

# Take customer ID as input to search
print("Search customer ID:",sys.argv[1])
name = sys.argv[1]
# Search the ID in the dictionary
for customer in customers:
    if customer == name:
        print(customers[customer])
        break

How do I solve this issue?我该如何解决这个问题?

The python command is effectively a wrapper to the system command, which calls the operating system's python command for you. python命令实际上是system命令的包装,它为您调用操作系统的python命令。

If the system's python executable is not on the system PATH, then obviously octave will fail to find it.如果系统的python可执行文件不在系统 PATH 上,那么显然 octave 将无法找到它。

Judging from the error I'm guessing you're on windows?从错误来看,我猜你在 windows 上? In which case, look up how to add python to your PATH in windows.在这种情况下,请查看如何将 python 添加到 windows 中的 PATH 中。

(eg here's a random first link from a duckduckgo search: https://datatofish.com/add-python-to-windows-path/ ) (例如,这里是来自duckduckgo 搜索的随机第一个链接: https://datatofish.com/add-python-to-windows-path/

Alternatively, you can use getenv("PATH") to check what your current system path is, and modify it from within octave to concatenate that string with the python executables directory, and then set it using setenv("PATH")或者,您可以使用getenv("PATH")检查您当前的系统路径是什么,并在八度范围内修改它以将该字符串与 python 可执行文件目录连接,然后使用setenv("PATH")设置它

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

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