python console closes before it starts - python

I am learning Python and came across this problem. I know that i can put "input()" at the bottom of the program, but the problem is that it IS there, but console terminates nevertheless. It seems like the programm never starts at all. I am using the 3.5 version of Python.

Open a terminal, navigate to the directory of your file, and type python myfile.py. If you just double click the file in your system explorer it will run and close when it finishes, which is often too fast to see what's going on.

I think you are starting the programme by clicking on it don't do that to run your programme follow following steps
1.open command prompt
2. go to the path where you have your python programme
3. use command for python for eg: python programme.py in linux
4. if it says python command not recognised probably because the path to python executable is not present in system path add it to environment variable and that should work fine

Related

Run Python on Notepad++, execute screen

https://medium.com/never-too-late-to-machine-learn/how-to-step-by-step-setup-notepad-to-run-python-scripts-e1ce3b3ac7fe
I am reading from this tutorial linked above.
What I am doing is trying to run python for the first time on Notepad++. I've downloaded python, etc.
What I am lost on is this:
"Just copy the location of the python exe file, and let’s go back to Notepad++. The execute screen is waiting for us.
In the execute screen, paste the location of Python followed with “$(FILE_NAME)” "
What is the execute screen, where am I pasting the location to?
I hope someone can help me. Thank you.
first, you need "$(FULL_CURRENT_PATH)" for full path of file, the $(FILE_NAME) is for file name only or relative path
then you can paste like
C:\Python39\python.exe "$(FULL_CURRENT_PATH)"
for execute screen, new name for the menu is Execute NppExec Script.. see image below
to run your python script in notepad++ is quite simple:
make sure your python is correctly installed, open your console and type python, you should see something similar to the following (it should say Python X.Y.Z accordingly to the version you have installed)
now that we know that python is correctly installed, go to notepad++, open on it your desire script and go to the Run menu and select Run...
enter the following command python -i "$(FULL_CURRENT_PATH)" and press Run
You should see the following
And that is all.
-
Now that the previous worked, lets make it more reusable, repeat step 1 and 2, but instead of Run press Save..., give it a name (like python for example) and if you so desire also a keyboard binding (a key combination to press to run this command)
Now in step 1, you can pick python (or whatever you name it) instead or "Run..." to run you script (or press you key combination if you give it one)
now some additional explanation about the previous command python -i "$(FULL_CURRENT_PATH)"
the -i option is to enter in interactive mode, that is run you script and then continue executing python so you can do more python stuff with everything for your script loaded in there too, that way the console doesn't just close intermediately after your script is done running.
$(FULL_CURRENT_PATH) is a command for notepad++ to said to substitute there for the full path of your given script
Alternative command: cmd /K CD "$(CURRENT_DIRECTORY)" & python -i "$(FILE_NAME)"
This command is the equivalent to opening your console (cmd /K), move with cd to the folder where your script is (CD "$(CURRENT_DIRECTORY)") and then run your script with python in interactive mode (python -i "$(FILE_NAME)") and & is to separate both instructions (the /K is like python's -i but for the console) (the "-i" is now optional, but I prefer it with it to test other stuff in the script without need to put it on the script because doing so make little sense...)
Why you might want to use this over the other? two reason:
the first is when dealing files so you don't have to type the full path of a given file that sit next to your script so you can use just the name of said file, aka set the working directory to that where the script is located, otherwise it will be that where notepad++ is located (usually it might be like "C:\Program Files\Notepad++").
In case of an error, the windows will remain open so you can know what was the error instead of closing abruptly.

VS Code trying to run .../Activat.ps1 in cmd terminal

I'm using VS Code for a Python project using a virtualenv. I switched my deafult terminal from powershell to cmd as VS Code was not happy executing powershell scripts.
Now when I open a terminal in my project it opens cmd (as desired), but automatically tries tor run .../Scripts/Activate.ps1, which it doesn't like. I want it to run .../Scripts/Activate.bat as we are in cmd. Runnning it manually for now, but would be nice if I didn't have to.
No doubt there is a setting somewhere to change this, but I cannot find it. Any ideas?
This is a problem related to the Python extension, it should be fixed in the last update.
You can get some information from here.

how to execute python script on atom on windows

I am using Atom on Windows 10. While setting up Atom on my computer, I created a folder called "beyond basics". Then I created a python file. I installed platform io on Atom. i got a "+" icon on screen. upon clicking that i got a command line. I am trying to execute on that by writing python filename but I am getting an error. Any help is appreciated.
python3 myfile.py
Try typing myfile.py without the python prefix. It may work, as it works for me on Windows 10. Your bubble is covering up an error message that could help use debug. Can you add an edit and tell us the error message? Until then, just try the command without the python prefix.
You should also save before running, as was commented by Denis Fetinin.
If it still doesn't work, try addding python to the env variables. It's a simple process that you can follow here.

Running a simple script from desktop [duplicate]

This question already has answers here:
How do I run a Python program in the Command Prompt in Windows 7?
(24 answers)
Closed 7 years ago.
So I'm an extreme beginner to programming, just starting the Python class on Coursera. Using Python 2.7.10
Anyway, I made a simple print statement script in Notepad++
print "Hello World"
and saved as a python file on my desktop
newprog.py
However when I try to run it a cmd window appears and disappears and I'm not quite sure whats wrong.
The other question that this was linked as a duplicate to is about accessing python through the command prompt, which I don't have a problem with. From answers given it is now apparent to me that my dilemma was due to an erroneous belief that the interpreter would remain open after running whatever script I wrote.
Sounds like your program simply opened, ran and exited. So nothing was wrong, it just all happened a bit quick for you to see it.
You should run it from a command prompt or get an IDE like Pycharm, which will allow you to both write and run your script in one program.
To run from command prompt, use either Windows Key + R and type 'cmd' or click start and type 'cmd' into search box. Then you can drag your script to the command prompt window and press Enter to run it.
If you wanted to run it by double click, you'd need something to stop it from finishing until you'd read the message. To achieve this you can use the raw_input function, which waits for user input.
So your script would then look like
print "Hello World"
raw_input("Press Enter to exit")
Then you could double click and press enter when you are ready to exit.
Go to the command prompt window
python
then type in
execfile('path to newfile.py here')
Your file will now be executed
I'm running python 3.4.3. But it should be the same, I hope.
Go go "..\PythonXX\Lib\idlelib" and look for idle.pyw NOT idle.py and using the you're able to execute simple one line commands like the one you have up there.
From that you can also create a new file and do more complicated stuff.
If you create a shortcut to your desktop, you'll be able to access it easier.
Let me know if it helps, or at least correct path.
Your script is probably working and then finishing, the result is shown but not for long. I recommend opening the console and running your script from there, or you could use a simple batch file to run python scripts and then wait for a key press.
To open the console you can use the Windows key along the R key, Win-R (to run a new process) and write cmd, or you look for cmd in your Window's start menu.
With the console opened, you must locate the path where your script is, you can use the cd (Change Directory) to get there, for example:
cd C:\Users\your_name\Desktop
and then write:
python newprog.py
to run your script.
Another option is to use this simple batch file (save it as python34.bat or similar, but the extension must be .bat, put it wherever you like):
#ECHO OFF
C:\Python34\python.exe %*
pause
#ECHO ON
And then use that to run your scripts by right clicking a python script file, open with (run with) and use this batch script as default (if you want). Also, if you have another version of Python, or is installed elsewhere, you must change the "C:\Python34\" part.
This is a computer we're talking about here. It might take you triple the time it takes a computer to multiple two numbers for example. With this notion in mind, the computer quickly prints then exits.
raw_input() # at the end of script wait for user to supply input, delaying script exit

Having trouble running python in cmd.

I am a complete noob when it comes to programming. I just downloaded python and I will be using Notepad ++. I have saved a file to my desktop and the file name is test and changed the extension from .txt to .py
So when I go to Notepad ++ and create a program and save it, I go to the cmd prompt making sure I am in my desktop directory and type the following
python test.py
and it tells me that python is not recognized. Any help to fix this problem would be greatly appreciated.
First thing python is indent oriented programing language and it comes with its default editor called IDLE. So if you use notepad++ instead of IDLE it might gives you a syntax error. Second thing for executing python file from command prompt,you need to setup environment variable.Please see below link for setting up environment variable.
https://docs.python.org/2/using/windows.html
You can directly run your program in IDLE without using command prompt. So i would suggest you to use pythons built in editor (IDLE)

Categories

Resources