Cannot run a Python file from cmd line - python

I have installed Python and written a program in Notepad++.
Now when I try to type the Python file name in the Run window, all that I see is a black window opening for a second and then closing.
I cant run the file at all, how can run this file?
Also I want to tell that I also tried to be in the same directory as a particular Python file but no success.

I assume you are running the script with command python file_name.py.
You can prevent closing of the cmd by getting a character from user.
use raw_input() function to get a character (which probably could be an enter).

It sounds like you are entering your script name directly into the Windows Run prompt (possibly Windows XP?). This will launch Python in a black command prompt window and run your script. As soon as the script finishes, the command prompt window will automatically close.
You have a number of alternatives:
First manually start a command prompt by just typing cmd in the Run window. From here you can change to the directory you want and run your Python script.
Create a Windows shortcut on the desktop. Right click on the desktop and select New > Shortcut. Here you can enter your script name as python -i script.py, and a name for the shortcut. After finishing, right click on your new shortcut on the desktop and select Properties, you can now specify the folder you want to run the script from. When the script completes, the Python shell will remain open until you exit it.
As you are using Notepad++, you could consider installing the Notepad++ NppExec plugin which would let you run your script inside Notepad++. The output would then be displayed in a console output window inside Notepad++.
As mentioned, you can add something to your script to stop it completing (and automatically closing the window), adding the line raw_input() to the last line in your script will cause the Window to stay open until Enter is pressed.

Try to open in Command Prompt instead of run window. The syntax is:
py filename.py
If it doesn't work, try to reconfigure Python. Did you set environment variables? If not, this could help you

Related

Python IDLE 3.9.1 file not opening in windows

I had saved a python file after working on it for sometime, but now when I open it, Python 3.9.1 opens a window then immediately closes. I had done lots of work on this and don't want it to go to waste. I'm on Windows 10.
If you’re using the Open option when you right-click or you are simply double-clicking the script, the program will run and close so fast you won’t even see it.
Here are two options I use:
Open up the Command Prompt. You can easily do this by going to the address bar of your File Explorer and enter ‘cmd’. If you’re in the directory where your script is, the current working directory of the Command Prompt will be set to that. From there, run python my_script.py.
Edit your script with IDLE. If you’re using an IDE, it should be nearly the same process, but I don’t use one so I wouldn’t know. From the editor, there should be a method for running the program. In IDLE, you can just using Ctrl + F5.
Right click on it and clicken "open with". Then choose Python IDLE.
You are trying to run the file instead of editing it.
You have to right-click the file and you should the "edit with idle" option.

How to make Windows execute my Python script files when I click them

I have made a Python program that uses output.to_csv('results.csv'). When I run it in Spyder it creates that CSV file. But when I double click on it nothing happens. Cmd appears and closes but nothing on the folder, no results.csv.
What am I missing? What more do I have to do?
Run the program from the command line itself instead of double-clicking the .py file.
I assume you are on Windows since you mention CMD. First, cd into the directory containing your program. Then, run python <program>.py or python3 <program>.py depending on your installation.
This time, you will see any output or error messages that appear in CMD without it immediately closing.
If the .csv file really exists, you should be able to go to your File Explorer and find the file at the top of the "Quick Access" section. Right-click the file and hover over "Open With >". Then select Notepad and a notepad will open up showing your results.
If you do not see the file, then try running your program on the command prompt (for Windows):
Press the windows key and type "cmd" in the search bar.
Choose "Command Prompt"
Go to the dir of your program using the cd command
Type python <program name>.py
If there are no errors, follow the steps in the first paragraph.
Ok i guess windows is not recommended at all for this type of tasks. I mean running something simple as create such file is like trying to kill the Lernaean Hydra.
What i did is i just runned it with anaconda prompt and it worked sweet! Thanks for help. Thanks to all!
PS: I'm seriously considering changing to Linux after this
For anyone having the same problem, but have anaconda installed. 1) Open Anaconda Prompt, 2) use cd (1 space) then adress of the folder which contains your py program (eg. cd C:\Users\Bernie\Desktop\tasos) and hit enter, 3) on the next line that appears type: python program_name.py, 4)Hit enter, 5)success!

Python.exe opens in a new console window

I used to run Python scripts from my Windows command line, and all the prints were printed in the same console. Now something happened on my machine (Windows 10), and when I launch a Python script from the command line (i.e. open a Command Prompt and run python <my_script.py>), Windows opens a new window (titled with the absolute path of python.exe). This windows closes automatically at the end of the execution, so that I can't see the output.
How do I go back to printing output in the same command prompt window from which I run the script?
Not sure how useful this will be but I had this same problem, found this thread, and realized that the new console window was opening up when I omitted 'python' from the command.
>python myscript.py
shows the output right in the terminal where I typed the command, but
>myscript.py
opens the new console window and closes it immediately after the script runs.
It's odd but it very likely a windows setup issue as python is an exe. If memory serves windows will spawn on a > run command so checking the way python is booting will help.
Unfortunately it could be a range of issues, so some steps towards victory:
What happen when you just type python into the cmd? If it simply starts the input >>> - it means your python setup is fine. If a cmd window spawns and disappears it may be a windows permissions issue.
Try running your script with -i flag: python -i script.py. This drops you into the repl when the app completes - displaying your output.
Ensure you're using the native flavour of the cmd to test. Ensuring any command app or IDE isn't injecting a start command or weird /K (spawn new window) flag.
Hope it helps.
In my computer this was caused by Windows not knowing what program a .py file was associated with. I solved this by going to:
Control Panel -> Programs -> Default Programs -> Associate a file type or protocol with a program (Scroll down) and choose "Choose default apps by file type" Scroll down until you see ".py" and choose the correct
Python interpreter.
Simply: last row on the end of your program maybe this:
input("\nIf you whish end the program press any key ...")
...and your program wait for the key and you see your outcome

Python py not executing in other PC

I have a technical questions on python script.
I have developed code on my laptop and I want to move it in another one.
I installed python there, put when I try to double click on the .py file it is not working. Like the cmd screen goes away in a second.
Do you know why?
(if I open python through cmd it is working, so it is in path and it works)
What is probably happening is that there is an error being thrown on your new computer which causes command prompt to just instantly close.
The best way to run a python script is from an open command prompt/terminal. To do this open a command prompt and move into the directory of your python file. For example, if the file you are trying to run is located at C:\Users\Davide\PythonScripts, then open a command prompt and type
cd C:\Users\Davide\PythonScripts
Now your command window is in the folder that you want to run files out of.
Next you want to tell Python to run your script. This can be done by typing "python ". For example, if your script is name my_script.py, you would type
python my_script.py
What this is doing is telling your computer "open an instance of Python where you are running my_script.py." If there are any errors thrown, the command window will stay open after python closes and you can see what is going on.
Most likely, there is a package you are trying to import which it cannot find because it was not installed on your other computer. If at the top of your file you have "import xxx" or "from xxx import yyy" lines, your other computer might not be finding those modules and just throwing an error that instantly closes command prompt when you just double click the .py file.

Python command-line mode error

I have some difficulties trying to run the python programs from cmd, or command-line. When i double click the python program it display the cmd for some seconds in fact less than 3 Seconds, and cmd disappeared. I can only run by right clicking on it then edit in idle and run model command. Only from the python shell. And how to give path for python shell to find applications, for instance just type the program name and it runs. Do python have GUI as exe programs?
First make sure python.exe is in your path.
Then type in the command prompt (cmd) python "path\to\program.py"
If you want to just type the progam name, you have two options.
Add the folder containing your program to your path.
Right click in the folder while holding shift, and then click "open command prompt here"

Categories