Reoccurring Python error that doesn't make much sense - python

I keep getting this error while working in a script:
'python' is not recognized as an internal or external command,
operable program or batch file.
Isn't this an error you'd see in terminal? I'm in Atom text editor. On top of that, it was working a few minutes ago (i.e. giving me errors about my actual code) and I've been working on it with no problem all day long.

Reinstall Python, and make sure you check the 'Add to Path' option.
You could also try adding Python to the path variable (Tutorial).

Related

FloPy: 'head' is not recognized as an internal or external command, operable program or batch file

I am trying to run the flopy3_modflow_boundaries example from the FloPy Jupyter Notebook. The notebooks have worked perfectly well for earlier examples (including building, displaying images, running a MODFLOW-NWT model, and viewing the results...so I think have things substantially set up correctly), but for some reason when it gets to the following section of code:
!head -n 10 'data/test.riv'
I get the following error:
'head' is not recognized as an internal or external command,
operable program or batch file.
I'm not sure what the "!head" code with the exclamation mark is supposed to do, or how I can fix the error. If it matters, I'm running Python 3.9 on Windows 7. "Head" is a groundwater term, so I assume it is imported from FloPy in the first step of the notebook?
Thanks!
head is a Unix/Linux shell command to show the first n lines in a file. So running head -n 10 'data/test.riv' would output the first 10 lines in file data/test.riv. Note that when you precede a command with ! in a Jupyter cell, it runs the command as it would in a terminal, and not as Python code.
You can do any of the following:
Run the code on a Unix/Linux machine which has the head command.
Skip the command as it probably will not affect the rest of your code as it is meant to just show you the first 10 lines in the file, which you could just do by opening the file in a text editor.
Replace the command with an alternative command for head in Windows to perform the same function, although AFAIK, there isn't a direct equivalent.

Why does my VS Code not recognize 'python' as an internal or external command

so I just considered using VS Code for writing python as i'm settling into programming. After following the full guide here: https://code.visualstudio.com/docs/python/python-tutorial and everything seemed ok.
But my code wont run, but runs well in my PyCharm but on VS Code it pops this error below:
"'python' is not recognized as an internal or external command,
operable program or batch file."
I have followed threads here advising reboot of computer and all but its not working for me, I would love to at least know what the problem is because my decision to use VSC is because i'd like to have one IDE that supports more languages so that write in one place or at least a few IDEs.
Any help will be appreciated.
Thanks in advance.
thanks, I've fixed it by uninstalling the existing python and reinstalling latest version, most importantly i realized i was adding a new path instead of editing the existing path. In recent windows clicking edit on the existing path will open a new dailog where you can then add a new path, that will be added to the existing path. Then I restarted my computer and then typed python --version at command prompt and there it was :) by the way this link (geek-university.com/python/add-python-to-the-windows-path) was most helpful but you have to do it as i described.

Trouble installing path for Python 3.7 on Windows 10

When I try to enter 'python' into my cmd window, I get back this error: "'python' is not recognized as an internal or external command,
operable program or batch file."
Any ideas how to get my cmd window working?
I tried going into environmental variables and adding my python path to the path system variables, however this is not working.
python
'python' is not recognized as an internal or external command,
operable program or batch file.
This might be what you are looking for
Running path can display the system path, and ensure things are configured correctly :)
Answered over here
Assuming you googled and found that (or something similar) yourself, then my guess is you're running into a different issue. For example, the semicolons in the path, especially the proper positioning of them, are very important, could your issue be that simple?
Try entering:
py
Some systems use this to function like mine. I had the same error but then I tried py and it worked. Maybe this will work for you

How do I keep a console window open after my python program terminates to see any errors produced?

I am using python 3 on Win10 and running my code by opening a Command Prompt window and typing the file location. However, the window closes as soon as the program terminates, and before I can read any errors.
Edit: This happens whether or not the program has errors.
Thank you.
Solution 1:
I just saw your comment:
When I do that, I recieve the error 'python' is not recognized as an
internal or external command, operable program or batch file
It looks like you haven't specified the path to the python executable: you need to add the python executable path to your Window's PATH variable. You can see how to do that here: Add Python to the PATH Environmental Variable (‘python’ is not recognized as an internal or external command)
Solution 2:
You can use input("enter to exit") at the end of your python code to keep the program alive. It would exit once you press enter.
You could also surround your code in a try except statement and place thr input() in the except to prevent the program from exiting when there are errors, but like #Kevin mentioned in the comments, this would catch run time errors but not syntax errors.
Solution 3:
You can write errors or anything information you want to a file such as log.txt for example, and then read that log file once the code finishes running e.g. how to write to a file in Python
FWIW, I have several Python versions on my Windows system, so I don't want to add any Python directories to my path permanently.
Code for each version is in a separate folder (e.g. 'py37'), with a subfolder for each project e.g. 'myProject'.
In py37, there's a batch file called pyEnv.bat with this content:
#echo off
path=%path%;C:\Python37\;C:\Python37\Scripts\
cd.
cmd
In Windows explorer, I head over to the project folder I want to work in, click in the address bar and type ..\pyEnv. That launches a DOS-box, in which I now can do python myproject.py. You can see print() output, errors, and so on.
You can up-arrow to try different modules, having typed them first.
Once you quit the DOS-box, your path is back to normal again.

Issue with Python Batch file to run Python through Notepad++

EDIT: The code I wrote in my Python file was just this:
print "foo"
I'm using Windows XP Home Premium on this tiny little HP Mini 1000, and I want to run Python files, since we're learning it in school. I am aware of this topic, so I tried to run Python files using a batch file (python.bat), and I'm getting an error that says, "Can't find 'main' module in ''" whenever I run the batch file. I followed the instructions given here. All I did was change "Python26" to "Python33" because of the difference in versions.
Any idea what's wrong here? I really want to run Python files from Notepad++, so I don't want any alternative ways to run them.
This sounds like you don't have PYTHONPATH set up correctly. I suggest you review the documentation here:
http://docs.python.org/2/using/windows.html
Instead of calling Python, call cmd.exe and then use the set command to inspect which variables are set and how they are set. Run the exit command to leave the command shell. When you think you have the variables set up correctly, try again to run Python.
Good luck and have fun!
I use the command line interpreter or IDLE mostly (Win 8.1 now, but I've done so since Win XP SP2), but NPP is my main text editor, so I was curious about this issue.
When I was reproducing this, I was able to generate several errors, but the only one I got that was an exact match was when I failed to configure the Run option correctly.
You need to make sure to follow this step exactly in the instructions you were following. When you navigate to Run -> Run in Notepad++, you have to enter this exactly:
C:\Python33\python.bat "$(FULL_CURRENT_PATH)"
I am pretty sure you left out the "$(FULL_CURRENT_PATH)", or otherwise didn't add it correctly, as failing to do so causes exactly the same error on my end. Failing to include this means that when you run the batch script, you get the wrong input to the Python interpreter, causing the error.

Categories