Python fails silently - python

I am trying to get started with Python, so I installed Python 3.8, from python.org (on Windows 10). I remembered to check the "Add to PATH" during the installation and I have confirmed that it has been added to path, as seen in the first image.
However, when I try to use any commands, whether it be python --version or python HelloWorld.py, nothing happens. I have tried both the traditional command line as well as Powershell. I have also tried replacing python with python3, the result is the same. As you can see in the second image, I get no errors, just a blank line. So it's not that it cannot find Python at all, it rather seems that something is wrong with the installation.
Have anyone else encountered this kind of behavior? I have tried re-installing Python as well as removing old installations.
Image 1: Python seems to be correctly added to PATH.
Image 2: Python fails to return any output. But also no error. Same thing happens with python HelloWorld.py.
Update 1 - Here is what I have tried so far:
Changing PATH to refer directly to exe file.
Using both Command Prompt and PowerShell.
Rebooting the PC.
Re-installing Python (including removing old versions).
Both the python and python3 command.
Update 2 - NameError: name 'python' is not defined
Update on the update: This was a wild goose chase. You are not supposed to be able to use that command in the interpreter, as described in this post.
So I tried to use the console from the python.exe file instead, and I got the following answer. However, from what I can find on it, it's normally a problem you encounter on elements of the code (like print()) and not on the python command...?
(image removed)
Update 3 - It works (kinda)
If I use the command py --version or py test.py it works. I have no clue why though...

After extensive research, I still cannot find an answer to my own question. But I have found a work around, which minimizes the consequences:
Instead of using python, simply use py.
This will invoke the Python launcher instead of Python itself (from what I've read). For most people this will be good enough, but it is not the same. So it might cause issues and version mismatch in certain scenarios.
Also, despite of this weird behavior, Python seems to run fine in Visual Studio Code, when using the "Run" button (or the py command in the terminal).

Related

Python.exe works with -v or -h but will not execute a file. What is wrong?

The python programs on Windows have stopped executing scripts.
It will run python. For example, I can check the version and get the help text.
I've tried running it from "c:/User/.../python.exe", `py -3, etc.
I've also installed all Windows updates and re-installed python. (from version 3.8 to 3.10.7; same issue for both.)
Also, I've tried it from both power-shell and cmd.
I have tried a variety of scripts, but I have a test.py which just prints something.
Also, I've tried py -3 -c 'print("Hi")'. It does not work either.
All of these do not print any diagnostics. I simply get the shell prompt on the next line. I can also tell that the script is not even loaded. For example, if I give a filename that does not exist it does the same thing; no error message. The same occurs if I add a syntax error to the file.
I am seriously stumped. What could cause this?

VS Code can't find Python and interpreter

I searched everywhere, but did not find an answer. Since I think the answers are outdated because they use python.pythonPath and this does not exist anymore.
My problem is the following:
But I installed Python#3.10 already via homebrew (I have a mac M1 with macOS Monterey).
If I use ,,which python3'', I will get:
And here you can see the output of ,,echo $PATH'':
What should I do, so that the error message does not appear again??
You have to click on 'No Interpreter Selected' and then you can add a path to python
If nothing else will work
Go on terminal and write your python-path\python.exe python-file.y.
If this works you can create a shell script inside of your folder and then put inside the python call. Then you can start your python file with the shell script.
You can go to the Extensions Library and download the Python Interpreter for VS Code. Make sure Python is installed on your PC.
If you still have problems, try reinstalling Python and VS Code.
I think the above two answers have been well analyzed. If you can't solve the problem, you can refer to the instructions of vsocde and reinstall python.

Python and VS Code, messy paths in terminal

I've just started with Python, I've installed Python and it's extension from Microsoft, and when I run Python file in terminal I get the results, but the ugly path is covering plenty of space in terminal (like on the photo below). I've found video how to remove that from debbuging console, but couldn't find anything related to normal terminal. Is there any solution for that?
I am using Windows 10 and newest version of VS Code.
https://i.stack.imgur.com/vjKOm.png
You need to do 3 things.
1.
You need to add Python to your PATH. Call this python2 if you want to use python 3 as well for other project.
(However if you are starting, considering using python 3 from the beginning)
2.
Just use the file name if you are already in the folder
Your command is reduced to this then:
python2 strings.py
3.
Use prompt to change the line before your command. See doc for more information: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/prompt

VS Code continuing to run a Python script using an outdated Python interpreter, even after specifying intended interpreter

So using the Command Palette, I specified my intended Python interpreter already. This is reflected correctly in the status bar on the bottom left.
However, when I run the code above, it attempts to use another Python interpreter. This is clearly reflected in the Python error - it tries to run a Python 2.7 interpreter, and so it tells me that I need to install xlrd, even though I already have it installed in the right Python interpreter.
Can't seem to find anyone else having similar problems. The exact same code runs fine in another IDE like Spyder.
When i do:
import sys
print(sys.version)
It shows me the right version.
My code actually runs when I run each cell individually. It's when I run everything together (Ctrl-Alt-N) where I start seeing this error.
It's because you're executing code using the Code Runner extension instead of the Python extension. If you disable Code Runner for your workspace and then use the Python extension (the Play button will be green in this case) then it will use the interpreter shown in your status bar.

Downloading python 3 on windows

I am currently trying to figure out how to set up using python 3 on my machine (Windows 10 pro 64-bit), but I keep getting stuck.
I used the Python 3.6 downloader to install python, but whenever I try to use Command Prompt it keeps saying "'python' is not recognized as an internal or external command, operable program or batch file" as if I have not yet installed it.
Unlike answers to previous questions, I have already added ";C:\Python36" to my Path environment variable, so what am I doing wrong?
I am relatively new to python, but know how to use it on my Mac, so please let me know if I'm just fundamentally confused about something.
In environmental variables under path, add your python path... you said you already so please ensure is their comma separation between previous path..
And once added save environment variables tab. And close all command prompt then open it.
Then only command prompt will refresh with your python config..
Main thing, if you enter python which mean python 2.
For python3 type, python3 then it should work
Why are you using command prompt? I just use the python shell that comes with IDLE. It’s much simpler.
If you have to use command prompt for some reason, you’re problem is probably that you need to type in python3. Plain python is what you use for using Python 2 in the command prompt.
Thanks everyone, I ended up uninstalling and then re-downloading python, and selecting the button that says "add to environment variables." Previously, I typed the addition to Path myself, so I thought it might make a difference if I included it in the installation process instead. Then, I completely restarted my computer rather than just Command Prompt itself. I'm not sure which of these two things did it, but it works now!

Categories