-3

Just dipped my toes into Python this morning and I'm trying to pare down how long of a directory I have to input to run my *.py programs.

My Python install directory is C:\Python\Python39

My work folder is C:\python_work

I made a file called hello_world.py and would like to run it directly from the C:\python_work folder, but it won't let me. It just returns C:\python_work instead of "Hello Python World!".

The book I'm following says I should be able to type C:\python_work>python hello_world.py. That doesn't work. I have to type C:\python_work>C:\Python\Python39\python hello_world.py.

How do I get python to run directly from the python_work directory? I hope what I've written has made sense.

Thanks!

EDIT: PATH option is selected on install. Did that before coming here.

  • 3
    Just open your directory in terminal and type `python hello_world.py` . Also See [What topics can I ask about here?](https://stackoverflow.com/help/on-topic) – Shivam Jha Oct 07 '20 at 15:30
  • 1
    Does this answer your question? [Python Path Directory](https://stackoverflow.com/questions/47140580/python-path-directory) – Random Davis Oct 07 '20 at 15:34

4 Answers4

0

I think the issue you are having is that you do not have python added to your PATH.

You can add it to your PATH by using this set of instructions: https://www.educative.io/edpresso/how-to-add-python-to-path-variable-in-windows.

Or you may want to uninstall and then reinstall python ensuring you check the add to PATH button in the installer.

Fin HARRIS
  • 47
  • 4
0

What happens if you type python --version into your command line? If it returns an error, it likely means that you haven't added Python to your system path.

If you type variables into your windows search, you'll see the entry below. Click on that and add your python directory (C:\Python\Python39\) into the entry called PATH.

enter image description here

Once python is in your path, you should be able to open the python work directory and simply run it with the python command.

# in a new terminal

cd C:\python_work
python hello_world.py
Jacobm001
  • 4,032
  • 4
  • 29
  • 49
0

You could try the following:

  1. Write in command line: python --version the output must be some like:

Python 3.8.5

If appers:

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

You will need add Python to Windows Path variable Add Python to the Windows Path.

  1. To execute your program, you can try with:

> python C:\python_work\hello_world.py

or:

C:\python_work>python hello_world.py

Temo BE
  • 1
  • 1
-1

That's because you do not have python on the PATH. To add the python to path see this answer. Of course, you will be doing it for Python3 rather than 2, but the steps are the same. You can find another proper answer from the same topic.

null
  • 1,341
  • 8
  • 18
  • Ok. The installer itself added python to PATH. It was a selectable option. I verified via entering "variables" into the Win10 search bar. C:\Python\Python39 is the only entry. When I check "python --version", it correctly displays "3.9.0". This still could be a path issue, I simply don't know. But I'm still unable to execute through the python_work folder. One thing is that if I let Python do the install, it tucks it way down the AppData pathway which makes calling it a chore, so that's why I manually installed Python directly to C:\. Could that be an issue? – Phosphonothioic Oct 07 '20 at 16:32
  • If I let Python auto-install, it puts it behind C:\Users\*****\AppData\Local\Programs\Python\Python39. My end-goal is to shorten this. That's why I put it directly on the C: drive with a manual install. Still, adding to to PATH was selected. I don't know why I'm getting downvoted. I'm reading straight from a beginners manual that told me how to download and install/config. I think moving the directory to C: is causing the issue, but I'm simply not that versed to know for sure. Thoughts? – Phosphonothioic Oct 07 '20 at 16:41
  • My recommendation would be just try uninstalling python completely and then install [choco](https://chocolatey.org/install) (a package manager for windows) then execute your cmd as admin and then simply type `choco install python` it will automatically completes the all. – null Oct 07 '20 at 19:17