Pregunta

First, thanks in advance for any assistance.

I'm diving into Python and would like to continue to use Sublime Text 3 as I do for other projects. As I understand it, the internal version of Python is 3.3, but I'd like to use 2.7.6. Following other examples, I created a new build system as follows:

{
  "cmd": ["/usr/local/bin/python", "-u", "$file"],
  "file_regex": '^[ ]*File \"(...*?)\', line ([0-9]*)",
  "selector": "source.python"
}

When I switch to this new build, open the Console and then try to run the following command in the Python 2 syntax

print "Hello, world."

I get an "invalid syntax" error. However, if I try to run the same command in parentheses as required for Python 3

print("Hello, world.")

the command executes successfully. In other words, it does not appear that the Console is using the 2.7.6 build system.

What I find confusing is that if I save a new test.py file using the same Python 2 syntax as above, build it using the default Python 3 build system the Console outputs a successful execution of the print command – even though the syntax should not be compatible (as occurs in the Console). It seems I get different results running commands directly in the Console and running a build of file.

I'm sure this can be chalked up to a misunderstanding on my part, but I'd appreciate any thoughts.

Cheers.

¿Fue útil?

Solución

So, one thing you may not be aware of is that

print("Hello World")

is valid in both Python 3 and later revisions of Python 2, including 2.7.6, so if you're running your file from the command line, it'll execute properly regardless of which interpreter you're using.

You may also be confusing yourself regarding the Console in Sublime and the build systems. The Console, opened by hitting Ctrl` or selecting View -> Show Console, is running Sublime's internal version of Python, a stripped-down Python 3.3.3 if you're using the latest build. You cannot run Py2 commands in the console, no matter how hard you try. It is useful for running Python API commands to control Sublime itself, but it's of no use in building non-plugin files.

Build Systems let you run your programs through external compilers, interpreters, processors, or what have you. They are activated by choosing one in the Tools -> Build System menu, and then hitting CtrlB (or CommandB on OS X) to run it.

So, to verify that your build systems are working as desired, create a new .py file with

print "Hello World"

in it. Save the file, and select Tools -> Build System -> Python (the first one in the menu, between Make and Ruby). Hit CtrlB or CommandB as appropriate for your OS, and check out the pane that opens at the bottom of Sublime. It should show "Hello World" at the top line, then another line saying [Finished in 0.05 seconds] or something similar underneath it.

Now, select Tools -> Build System -> Python 3 (or whatever you named your new build system) and hit Ctrl/CommandB, and you should now see a traceback in the build pane for invalid syntax.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top