-1

I am newbie, pls don't be to harsh with me.

I am trying to setup Sublime Text with Python (for next Semester). Before that I used Haskell in SublimeText, where I could run my skript with "ctrl+b" in Sublime.

When I try doing the same with a file named "test.py".

  def add(a,b):

      return a+b

  main = print(add(2,3))

I get the error-message:

/home/nayooti/Desktop/test.py:1:1:
    **Parse error: naked expression at top level**
[Finished in 0.2s with exit code 1]
[shell_cmd: runhaskell "/home/nayooti/Desktop/test.py"]
[dir: /home/nayooti/Desktop]
[path: /usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games]

The "naked expression at top level"-part looks familiar, since it is very Haskell specific. Indeed, when I search the Web for exactly this message, it brings me to Haskell related stuff only. So apparently Sublime or the compiler thinks, I am trying to run a Haskell-script, even though I named the file ~.py .

For these, who are not familiar with Haskell and Python: you can usually run a script by: Python: main = print(method(x,y)) Haskell: main = print(function x y)

I am using Ubuntu 12.04. What am I doing wrong here? Help is very appreciated. Tx.

Jayanth Koushik
  • 9,476
  • 1
  • 44
  • 52
nayooti
  • 395
  • 2
  • 15
  • Tx. I've been there, but it's not. I know about Ctrl+B and when I use it, I get the error message I posted above. – nayooti Feb 13 '14 at 14:05
  • Have you set the build system? – Jayanth Koushik Feb 13 '14 at 14:06
  • As said, I am newbie, and I am not exactly sure what 'set built system' means. I guess Ubuntu comes with Python, and I am able tu execute via console. I haven't set any path in Sublime, in case that was your question. – nayooti Feb 13 '14 at 14:13

1 Answers1

5

Go to Tools -> Build System and make sure that Python is selected. Also, that is not how you write main in Python, it should be more like

def add(a, b):
    return a + b

def main():
    print(add(2, 3))

if __name__ == '__main__':
    main()

When you write

main = print(add(2, 3))

And load your script, that line gets executed since it is an assignment, much the same if I did

a = 1

Or

a = print(add(2, 3))

There is nothing special about the name main in Python. You still see your output because print has side effects, but rather than defining main you are just executing a script. The difference comes in if you were to try

add.py:

def add(a, b):
    return a + b

main = print(add(2, 3))

subtract.py:

from add import add

def subtract(a, b):
    return add(a, -b)

If you ran python subtract.py, you would still see 5 printed to the screen, even though there is no main defined in subtract.py. Obviously, this is not desired behavior, your main function in add.py should not be executing if you did not run it as the main script. When you run a Python script directly as python scriptname.py, there is a variable set global to that file called __name__ that gets assigned the string "__main__". If you import it, then __name__ is set to its qualified module name, something like "scriptname". This gives you the ability to define different behavior if the file is executed compared to if the file is imported by another script.

bheklilr
  • 53,530
  • 6
  • 107
  • 163