2

I have just started learning to code, and I can't seem to get my script to print in the command prompt. I have added python to the path and am certain I'm using the correct directory for the file. I have a python file "app.py";

def cube(num):
       return(num * num * num)


print(cube(4))

So in the command prompt I move to the directory with my script and type;

python app.py

And then nothing happens, I just get the command line again.

swaj
  • 23
  • 1
  • 4
  • Try to run `python app.py > a.txt` and check if there is some text inside a.txt. Are you running from standard command prompt? Or are you using any third party apps? – Alexander Santos Nov 19 '19 at 18:23
  • If nothing above works, try to add this line at top of your program: `import sys;sys.stdout=sys.__stdout__;sys.stderr = sys.__stderr__` – Alexander Santos Nov 19 '19 at 18:25
  • I am using the standard prompt, your suggestion returns the same result. – swaj Nov 19 '19 at 18:26
  • Do you get any response if you use `echo` command? i.e. `echo this is a test`. If so, please tell us the return for the command `where python` – Alexander Santos Nov 19 '19 at 18:29
  • I do get a response from that, the where python command returns `C:\Users\User_name\AppData\Local\Microsoft\WindowsApps\python.exe` – swaj Nov 19 '19 at 18:32
  • It seems you installed Python from the store... Am i correct? I recommend you to try installing from the official source https://www.python.org/downloads/ . Did you try this? – Alexander Santos Nov 19 '19 at 18:35
  • I honestly cant remember if I did or not, Ill uninstall and try it. – swaj Nov 19 '19 at 18:37
  • Ok, so I reinstalled python from the official site, and tried to run it in cmd again with the same issue persisting, also when running the where command returns the same path. – swaj Nov 19 '19 at 18:42
  • You should uninstall previous Python though, and i also recommend to mark "Add to PATH" checkbox, so you don't need to do it manually later – Alexander Santos Nov 19 '19 at 18:44
  • Yes I uninstalled python and python launcher from control panel, then I downloaded python 3.8 from python.org\downloads, I was givin no option to add to path? – swaj Nov 19 '19 at 18:48
  • Reexecute the installer, you may find it at the bottom of the installer. (It should look like that: https://docs.python.org/3/_images/win_installer.png but for your version and with "Add Python 3.8 to PATH" marked – Alexander Santos Nov 19 '19 at 18:50
  • I've reinstalled python however it seems that there are python files left in the windowsapps folder that are forcing the where python command to keep returning the old location. – swaj Nov 19 '19 at 18:58
  • Did you mark "Add to PATH" option? The command it runs is determined by PATH environment variable, it runs the first one that is equivalent to the command you used. You may need to re-open the Command Prompt to update it with current PATH though. If you didn't close the Command Prompt, try that. – Alexander Santos Nov 19 '19 at 19:14
  • Indeed that works, Thanks so much! – swaj Nov 19 '19 at 19:19
  • You are welcome :) I will create an answer based on our comments to help visitors. – Alexander Santos Nov 19 '19 at 19:23

1 Answers1

3

[Answer based on question's comments, check for details]

The problem's root was the Microsoft's Store Python, it was somehow broken. Reinstalling the Python by official website solved the issue.

For any other person who finds those problems, please follow those steps:

  • Uninstall the current Python and all its dependencies.
  • Close all Command Prompts open.
  • Download the official distribution from python.org
  • Install the distribution (Make sure that the "Add Python to PATH" checkbox is marked).
  • Try using python command again.

If this still didn't solve the issue, you can use where python to troubleshoot it. It will return the pythons it finds on PATH, the first one is the one that is executed. If there is another one, you may want to reexecute the Python Installer and re-mark the "Add to PATH" checkbox.

If the above ones still didn't help, you can try adding to PATH by yourself.

Alexander Santos
  • 1,458
  • 11
  • 22