print function does not show output on sublime text 3 console - python

I have started off with Python today and stuck with a weird problem. I am using Python 3.5.1 and Sublime text 3 and have written a basic hello world program.
print('Hello World')
Saved this file with the name python1.py and pressed Ctrl+B to execute it but the only thing that shows up on the console is [Finished in XYZs] which means that the program sucessfully executes but no output shown.
NOTE: I am using Windows 8(If that's important) and Python is installed properly and I can run my programs from CMD with no problems.

Solved it,
Press Ctrl+Shift+B after which a list pops up, select python from that list and its done. From next time just pressing Ctrl+B will work.
'Ctrl+Shift+B' Means "Build this code and then RUN it" instead of simply compile it

press Shift-Ctrl-B and select python. That happens because the program only checks if your python program is correctly. The next time just pressing Ctrl+B, will work

SHIFT-CTRL-B
CTRL-B ( next time onwards)

In my case I needed set Build System to Python.
Only it solved for me.

Related

Beginner question about finding Python script in IDLE shell

I'm a beginner in Python with no prior programming experience, so bear with me here.
I installed Python, started playing with it (typing variables, playing with math operations) in the Shell window and everything is fine. I open a New Window and started writing a simple script. Something like this:
print (1+1)
I press Run Module, and I am asked to name and save the script first. I do so by calling it firstscript.py, and save it to my desktop.
Now I press Run Module, and in the shell window 2 is printed on the screen. Everything is fine. I close Python, and open it up again. Now in the shell window, I type firstscript.py and I get the red message NameError: name 'firstscript' is not defined.
I can run my program only if I open the script file on my desktop and press Run Module from there, but if I try to start it up directly in IDLE Shell by typing its name, I get the error message.
What did I do wrong? Thank you.
Good to see that you are starting with python.
Firstly, you can run the file directly using 'Run Module' only when you have the file open. Once you save the file and quit, you are out of the file editor and back to the terminal.
Simply typing in firstscripty.py will not work as it does not recognize the command.
To run the file from the terminal, use the below code:
python [locationOfFile\]firstscript.py
You can check out this detailed explanation: https://realpython.com/python-idle/#how-to-work-with-python-files
The problem here is the Shell doesnt know that your firstscript.py is sitting on the desktop
The simplest way i suggest using cmd with:
python C:\Users\{your user}\Desktop\firstscript.py

What does "== RESTART <path> ==" in the IDLE Shell mean?

I have a simple script I wrote, and when trying to run it (F5) , I get this msg:
================== RESTART: C:\Users\***\Desktop\tst.py ==================
I restarted the shell, reopened the script, but still, the same msg appears.
I use python 3.5.1 and I tried to simplify the script as much as possible, but I still get this result. Now my script is only one line with a simple print(1) command and I still get this msg.
Was there something wrong with the shell installation?
I have a simple script I wrote, and when trying to run it (F5)
That's the hotkey for IDLE to run a file. It is not ordering to do anything. It's a log statement to explicitly declare that your namespace is being cleared and the file is going to be ran fresh again.
no, I didn't tell it to restart
But you did... You pressed F5
The same thing is happening with my shell. In the older versions, this does not happen. I've also noticed that when I press Python 3.5.2 Module Docs, I have my Internet browser open up and I see my directory being displayed onscreen. It looks like:
C:\Users\mycomputername\AppData\Local\Programs\Python\Python35-32\DLLs.
Is that suppose to happen? Is that secured? I don't know.
I've also found that this prints out whenever I "imported" something. So if I use an import command, and put it right before the line of my random name, it will print out that "RESTART" thing. It's always at the beginning. Or what it reads as the beginning.
CIsForCookies, my guess is that you don't actually have a complete script; maybe you have just a function definition and you haven't included a line to run that function. (I had this problem and then remembered to call the function I defined; the problem went away.)
You may have made the same mistake as me and ran a program and then you wonder why RESTART is all that shows up. My program was working perfectly I just did not print anything or ask for any input so it ran and was done with the program and nothing showed up.

python syntax error with the version 2.5 any idea how to fix it?

first i tried to run it by pressing F5. it said the syntax error is in the first line. i tried running the program from the command line but it said the same thing. any idea what's wrong or how to fix it ?
F5 runs the script in the current window, but the current window doesn't display a script: it displays an interactive interpreter (also known as a REPL: Read, Evaluate, Print Loop).
In this window, you simply type valid Python statements, and they execute immediately. There is not way, or need, to run the contents of the window.

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

Is there a command or a piece of code that prevents the Python(command line) from closing immediately after executing the code?

I need your help. I'm a newbie and I'm learning python. I know to write basic codes. But, when I execute the code in Python(command line) it closes immediately. Is there any piece of code that can prevent this from happening or a trick? Please help me out. Cheers!
P.S: I use Python 2.7 in Windows.
If you're seeing a Command Prompt open and immediately close when you double-click your .py file, that's to be expected - it's not how you're supposed to run a console-based Python script.
What you should do is start a Command Prompt via the Start menu, then run your program by typing c:\python27\python.exe myscript.py or similar.
Alternatively, use a Python IDE (eg. Idle) or an editor (eg. Scite) that can run Python scripts.
The usual way to do this is from outside of Python, as RichieHindle's answer shows.
However, if you want to create a .py script that you can double-click, and it will wait for a response before exiting, you can just add this to the end of your program:
raw_input('Press ENTER to quit')
(If you've written .BAT files before, this is similar to writing PAUSE in a batch file.)
python 2
raw_input('Press Enter To Continue')
python 3
input('Press Enter To Continue')

Categories

Resources