0

I'm trying to run a simple Python Script on the CMD but nothing happens when I run it. I get no errors or anything. The py script is just a simple print ("Hello World").

All my .py files are in the Python/Projects file.

http://puu.sh/FFfJm/ee63955506.png

Just some context regarding this: I did install Python then Pycharm then Anaconda. I don't know if that has anything to do with conflicting Python files.

nehry
  • 25
  • 1
  • 7

2 Answers2

0

python isn't in your path. Checkout Adding directory to PATH Environment Variable in Windows, which is a good reference to the question of adding a variable to the path in CMD.

Monte Roden
  • 61
  • 2
  • 5
  • These file names aren’t mentioned in the question, right? – faressalem May 02 '20 at 07:58
  • I've amended the screenshot to now say Hello World.py which is an actual py file with just a simple print statement. Still nothing prints. If I go back to D:\Python on the CMD where my python.exe is located, typing python -- version outputs "Python 3.8.0" – nehry May 02 '20 at 08:00
  • What does `python --version` output in the directory where your program files are? – Monte Roden May 02 '20 at 08:01
  • Try `D:\Python\python.exe "Hello World.py"` – Monte Roden May 02 '20 at 08:02
  • That command actually works! Thanks but i'm still baffled as to why it's not executing using the simple python command in CMD. – nehry May 02 '20 at 08:08
  • You need to add python to your path. Here's an excellent StackOverflow answer explaining it https://stackoverflow.com/questions/9546324/adding-directory-to-path-environment-variable-in-windows – Monte Roden May 02 '20 at 08:10
  • Prior to posting this, I actually did make a PATH variable to D:\Python\Projects using the environment variables. Still doesn't work but I'll try restarting my computer to see if anything changes. – nehry May 02 '20 at 08:18
  • You need to make the path variable `D:\Python` if `python.exe` is in `D:\Python` – Monte Roden May 02 '20 at 08:20
  • Hi, Thank you so much, me setting the PATH variable to D:\Python where my python.exe works. Now I can type Python "Hello.World.py" and it will now execute the script. Thanks so much for the help. – nehry May 02 '20 at 08:28
0

Are you sure you entered your command line query correctly?

python python_file_name.py

If you just entered your file_name.py it might have executed it but ended it simultaneously before you could see any result.

also try putting your file_name in double quotes.

rishi
  • 575
  • 2
  • 18
  • Hi, I inputted on cmd as "python Hello World.py" and nothing shows up. I also tried the double quotes but still no results. – nehry May 02 '20 at 08:04
  • When you type python on cmd does it display the python console? – rishi May 02 '20 at 08:08
  • If I type python in the directory where my python.exe is located (D:\Python), it will execute the Python Interpreter. However If i type it anywhere else, it will open my Microsoft Windows Store for some reason. – nehry May 02 '20 at 08:11
  • Did you create a virtual environment as save the virtual environment python as your default Python? – rishi May 02 '20 at 08:16
  • Just try reinstalling python from the official python site - python.org. Also add python to your path while installing. – rishi May 02 '20 at 08:17