Sublime Forum

Simple print("Hello") function does not work? Please help!

#1

Hi guys, I’m extremely new to ST. I actually just downloaded it (version is 2.7).
So, being in the process of learning Python, I decided to try out the editor by running one line.
print(“hello”)
This did not work when I built it using the Build tool in the menu. So I added “>>>” before “print”.
This still does not work.
Before I did either of these, I changed the Syntax to Python, so that is definitely not the issue. The error log is as follows:
Traceback (most recent call last):
File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py”, line 174, in _run_module_as_main
main”, fname, loader, pkg_name)
THIS IS ME, ASKER OF QUESTION. PLEASE NOTE THAT THE MAIN IS NOT BOLDED IN THE REPORT, IT JUST HAS TWO UNDERSCORES ON EACH SIDE OF IT
File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py”, line 72, in _run_code
exec code in run_globals
File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/py_compile.py”, line 170, in
sys.exit(main())
File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/py_compile.py”, line 162, in main
compile(filename, doraise=True)
File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/py_compile.py”, line 106, in compile
with open(file, ‘U’) as f:
IOError: [Errno 2] No such file or directory: ‘’
[Finished in 0.0s with exit code 1]
[shell_cmd: python -m py_compile “”]
[dir: /Applications/Sublime Text.app/Contents/MacOS]
[path: /Library/Frameworks/Python.framework/Versions/2.7/bin:/Library/Frameworks/Python.framework/Versions/3.6/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin]

0 Likes

#2

Did you save the file first? A relevant part of your error output is:

IOError: [Errno 2] No such file or directory: ''
[Finished in 0.0s with exit code 1]
[shell_cmd: python -m py_compile ""]

This would appear to be trying to run a file with no name, which I assume can only happen if you haven’t saved the file first.

If that’s the case, then you should save first, and make sure that you save it somewhere in your home directory as you may get permission errors if you don’t.

I assume you mean that your python version is 2.7, not your Sublime version, since the last release for Sublime 2 was 2.0.2. Just in case, make sure that you’re using Sublime Text 3.

1 Like

Python not working
#3

yes I’m sorry. my python version is 2.7 haha
I hadn’t saved it! I tried doing that right after I posted it, and I felt stupid for not doing that first.
Thank you so much!!
Can I ask you one more question?
When I built it when it was saved, before, it just said [Finished in 0.0s]
I assume this is the building finishing. Is there any way to run it within Sublime Text 3?
(I found out via Internet how to run it in Terminal, and it works there. I just am wondering if there is an in-ST3 way to run it)

0 Likes

#4

Building it from within Sublime should actually run it.

The most common reasons why it doesn’t seem to run your program would be:

  1. Your program doesn’t actually generate any visible output, so it seems like it’s not building but really it is and you just can’t tell.

  2. Your program is trying to gather input from the user (say asking them to type a number or their name or something), which is something you can’t do (out of the box) from Sublime.

  3. You accidentally selected the Syntax Check build system variant, so it’s not running your program, it’s just compiling it to ensure that it’s valid (in which case congratulations, your code is not broken ;))

For the first case, your sample program from above definitely generates output (i.e. the print statement). A common pitfall is to follow an online Python tutorial that assumes that you’re entering commands into an interactive Python process, where even just entering 1 + 1 will make 2 display. Here that doesn’t work (and can’t; see the next paragraph), so the only output that will appear is output your program specifically generates.

For the second case, you can’t run that kind of program from Sublime out of the box; it will display the output of your program as it generates, but it doesn’t allow you to interact with the running program. If you do this, your program seems to “hang” because it needs you to enter data and you can’t. This is likely not your problem since you see the build saying that it’s finished, which means it didn’t hang.

For the third case, you can verify that you have the correct build system selected by following the below steps. A symptom of this “problem” are files with names similar to your program file appearing in the same path (the output of the compiler compiling the file).

  1. Select Tools > Build System in the menu and make sure that it’s either set to Automatic or Python
  2. Select Tools > Build With... in the menu and make sure you pick Python and not Python - Syntax Check (this will display display this way regardless of how you finished step #1)
0 Likes