运行 Python 在命令提示符下
Running Python in a command prompt
我有一些 python 代码,我想从 cmd 提示 运行,但它不起作用,如果我的代码中有此语句,我的合作伙伴告诉我:
if __name__ == '__main__':
xs, a0, a1, y0, y1, ys = encode(sys.argv[1])
np.set_printoptions(precision=6, suppress=True)
那么应该可以运行。我会 post 我的整个代码和我的命令提示符错误,看看你们是否可以提供帮助
import random
import numpy as np
import sys
import pprint
from numpy import linalg as LA
#Takes in a n value
def encode(n):
xA0 = np.zeros((n+3,1))
xA1 = np.zeros((n+3,1))
xStream = np.zeros((n+3,1))
#Creates a random x
for i in range(0, n):
xStream[i,0] = random.randint(0,1)
#Creates A0 and A1 based on size
xA0[0,0] = 1
xA0[2,0] = 1
xA0[3,0] = 1
xA1[0,0] = 1
xA1[1,0] = 1
xA1[3,0] = 1
A0 = np.zeros((n+3,n+3))
A1 = np.zeros((n+3,n+3))
y0 = np.zeros((n+3,1))
y1 = np.zeros((n+3,1))
yStream = []
#Creates A0 and A1 using method defined in description
for i in range(0,n+3):
for k in range (0,i+1):
A0[i,k] = xA0[i-k,0]
A1[i,k] = xA1[i-k,0]
#A0*x and A1*x to get y0 and y1
for i in range(0,n+3):
y0[i,0] = np.dot(A0[i,:], xStream)
y1[i,0] = np.dot(A1[i,:], xStream)
#answers mod 2 to get real answers
for i in range(0,n+3):
y0[i,0] = y0[i,0]%2
y1[i,0] = y1[i,0]%2
#combined for yStream
for i in range(0,n+3):
yStream.append([y0[i,0],y1[i,0]])
print("x:")
print(xStream)
print("\n")
print("A0:")
print(A0)
print("\n")
print("A1:")
print(A1)
print("\n")
print("y0:")
print(y0)
print("\n")
print("y1:")
print(y1)
print("\n")
print("yStream:")
print(yStream)
return xStream, A0, A1, y0, y1, yStream
# This is only or when encode is used as a stand-alone module
# Read command line argument. Must be exactly one argument.
# It outputs on the console
if __name__ == '__main__':
xs, a0, a1, y0, y1, ys = encode(sys.argv[1])
np.set_printoptions(precision=6, suppress=True)
print("x:")
print(xs)
print("\n")
print("A0:")
print(a0)
print("\n")
print("A1:")
print(a1)
print("\n")
print("y0:")
print(y0)
print("\n")
print("y1:")
print(y1)
print("\n")
print("yStream:")
print(ys)
转到
My Computer > Properties > Advanced System Settings > Environment Variables >
在 Variable value
中添加 Python 的路径。
因为你已经安装到 D:\Python,将以下内容复制到编辑器中并将其保存为 bat 文件(比如 runpython.bat
),然后 运行 它(用感谢@eryksun)假设 Python 2:
@SET PATH=%PATH%;D:\Python;D:\Python\Scripts
@ASSOC .py=Python.File
@ASSOC .pyc=Python.CompiledFile
@ASSOC .pyo=Python.CompiledFile
@ASSOC .pyw=Python.NoConFile
@FTYPE Python.CompiledFile="D:\Python\python.exe" "%%1" %%*
@FTYPE Python.File="D:\Python\python.exe" "%%1" %%*
@FTYPE Python.NoConFile="D:\Python\pythonw.exe" "%%1" %%*
@SET PATHEXT=%PATHEXT%;.py
您现在应该可以从命令行运行 python 程序
我有一些 python 代码,我想从 cmd 提示 运行,但它不起作用,如果我的代码中有此语句,我的合作伙伴告诉我:
if __name__ == '__main__':
xs, a0, a1, y0, y1, ys = encode(sys.argv[1])
np.set_printoptions(precision=6, suppress=True)
那么应该可以运行。我会 post 我的整个代码和我的命令提示符错误,看看你们是否可以提供帮助
import random
import numpy as np
import sys
import pprint
from numpy import linalg as LA
#Takes in a n value
def encode(n):
xA0 = np.zeros((n+3,1))
xA1 = np.zeros((n+3,1))
xStream = np.zeros((n+3,1))
#Creates a random x
for i in range(0, n):
xStream[i,0] = random.randint(0,1)
#Creates A0 and A1 based on size
xA0[0,0] = 1
xA0[2,0] = 1
xA0[3,0] = 1
xA1[0,0] = 1
xA1[1,0] = 1
xA1[3,0] = 1
A0 = np.zeros((n+3,n+3))
A1 = np.zeros((n+3,n+3))
y0 = np.zeros((n+3,1))
y1 = np.zeros((n+3,1))
yStream = []
#Creates A0 and A1 using method defined in description
for i in range(0,n+3):
for k in range (0,i+1):
A0[i,k] = xA0[i-k,0]
A1[i,k] = xA1[i-k,0]
#A0*x and A1*x to get y0 and y1
for i in range(0,n+3):
y0[i,0] = np.dot(A0[i,:], xStream)
y1[i,0] = np.dot(A1[i,:], xStream)
#answers mod 2 to get real answers
for i in range(0,n+3):
y0[i,0] = y0[i,0]%2
y1[i,0] = y1[i,0]%2
#combined for yStream
for i in range(0,n+3):
yStream.append([y0[i,0],y1[i,0]])
print("x:")
print(xStream)
print("\n")
print("A0:")
print(A0)
print("\n")
print("A1:")
print(A1)
print("\n")
print("y0:")
print(y0)
print("\n")
print("y1:")
print(y1)
print("\n")
print("yStream:")
print(yStream)
return xStream, A0, A1, y0, y1, yStream
# This is only or when encode is used as a stand-alone module
# Read command line argument. Must be exactly one argument.
# It outputs on the console
if __name__ == '__main__':
xs, a0, a1, y0, y1, ys = encode(sys.argv[1])
np.set_printoptions(precision=6, suppress=True)
print("x:")
print(xs)
print("\n")
print("A0:")
print(a0)
print("\n")
print("A1:")
print(a1)
print("\n")
print("y0:")
print(y0)
print("\n")
print("y1:")
print(y1)
print("\n")
print("yStream:")
print(ys)
转到
My Computer > Properties > Advanced System Settings > Environment Variables >
在 Variable value
中添加 Python 的路径。
因为你已经安装到 D:\Python,将以下内容复制到编辑器中并将其保存为 bat 文件(比如 runpython.bat
),然后 运行 它(用感谢@eryksun)假设 Python 2:
@SET PATH=%PATH%;D:\Python;D:\Python\Scripts
@ASSOC .py=Python.File
@ASSOC .pyc=Python.CompiledFile
@ASSOC .pyo=Python.CompiledFile
@ASSOC .pyw=Python.NoConFile
@FTYPE Python.CompiledFile="D:\Python\python.exe" "%%1" %%*
@FTYPE Python.File="D:\Python\python.exe" "%%1" %%*
@FTYPE Python.NoConFile="D:\Python\pythonw.exe" "%%1" %%*
@SET PATHEXT=%PATHEXT%;.py
您现在应该可以从命令行运行 python 程序