0

I have some python code that I want to run from the cmd prompt, but it's not working, my partner told me if I had this statement in my code:

if __name__ == '__main__': xs, a0, a1, y0, y1, ys = encode(sys.argv[1]) np.set_printoptions(precision=6, suppress=True) 

then it should be able to be run. I'll post my entire code and my command prompt errors to see if you guys can help

enter image description here

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) 
18
  • 1
    How did you install python and did you allow it to add itself to your system path? Commented Mar 31, 2015 at 3:41
  • Try using py instead of python in the command prompt. Commented Mar 31, 2015 at 3:43
  • @Andy I think I just used an installer, and idk is there anyway I could check? Commented Mar 31, 2015 at 3:43
  • @Andy If he installed Python Commented Mar 31, 2015 at 3:44
  • Normally, the installer just installs to C:/Python Commented Mar 31, 2015 at 3:44

2 Answers 2

2

Since you have installed into D:\Python, copy the following into an editor and save it as a bat file (say runpython.bat) then run it (EDITED with thanks to @eryksun) Assumes 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 

You should now be able to run python programs from the command-line

Sign up to request clarification or add additional context in comments.

3 Comments

I run it as an administrator and it immediately closes out so I cant see what it says, but it didnt seem to work
@eryksun: fair enough, no argument with any of your points, I'll amend.
@eryksun: no @echo off. I find that when adding a new line to the type of file I omit the @ so I can see it is being executed. When I'm happy I prefix the @. Maybe its just me, but you have to let me retain some of my original :-)
1

Goto

My Computer > Properties > Advanced System Settings > Environment Variables >

Add Path of your Python in Variable value.

enter image description here

4 Comments

Would I just add D:\Python to varibale value? Because that's where my Python.exe is located
Yes, be sure you put ;.
I did it but for some reason it's still not working for me. I assume it's just like getting java to work though. not sure what im doing wrong
To make sure it worked, type python in command prompt or run. If it gives you python shell, it worked.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.