0

I use Sublime Text(the latest version) as the Python editor. But there are some issues. First, I have Python 3.6.1 on my MAC. But this division doesn't work properly in Sublime Text.

For example, when I type

print(3/2)

The result should be 1.5, but it shows 1 instead.

And another one, when I use set,

course = {'Math', 'English', 'Digital Circuit Design', 'Python'}
print(course)

The result is also a little weird.

set(['Python', 'English', 'Math', 'Digital Circuit Design'])

Is there some kind of configuration setting or something?

Update

When I build with the new build system, then it shows this following:

[Errno 2] No such file or directory: 'python3'

[cmd: ['python3', '-u', '/Users/L/Desktop/Python/Tutorial3.py']]

[dir: /Users/L/Desktop/Python]

[path: /usr/bin:/bin:/usr/sbin:/sbin]

[Finished]

Therminal

build system file

issue

Python version of terminal

Community
  • 1
  • 1
S. Li
  • 767
  • 1
  • 7
  • 14
  • 2
    You're not actually using Python 3.6.1. – user2357112 May 22 '17 at 18:32
  • Try running `import sys; print(sys.version)` in the same way you're running this - only then can you be *sure* what version you're using. – Arthur Tacca May 22 '17 at 18:37
  • @ArthurTacca It shows 2.7.10. That's the problem. But how do I change it? – S. Li May 22 '17 at 18:43
  • Since you can run `python3` from the terminal and it works but from within Sublime it says it can't find it, you may have a path problem. If you enter `which python3` in the terminal and it doesn't say`/usr/bin`, that's likely your final issue. In this case using this package (or the latest development release) may help: https://packagecontrol.io/packages/Fix%20Mac%20Path – OdatNurd May 22 '17 at 21:26

1 Answers1

3

Are you sure that you are using Python 3? Because Python 2 returns 1 and Python 3 returns 1.5 enter image description here


If your Sublime does use Python 2 build options (default is Python 2), you can change it to Python 3 like this:

  1. Tools > Build System > New Build System

  2. Use following configuration to declare a new build system.

    {
        "cmd": ["python3", "-u", "$file"],
        "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
        "selector": "source.python"
    }
    
  3. Build with this new system.

Done.

Read also: http://docs.sublimetext.info/en/latest/file_processing/build_systems.html


Extra troubleshooting from comments with help of AdamSmith:

If you get such an error: [Errno 2] No such file or directory: 'python3'

Try replacing python3 part of the configuration with full path. You can find full path by which python3 command.

Ferit
  • 8,692
  • 8
  • 34
  • 59
  • I'm sure. If I use the idle editor, the result is also 1.5. This issue just happened in Sublime Text. – S. Li May 22 '17 at 18:32
  • Does Sublime run python code itself? Or you run it manually from terminal? If Sublime runs, check which binary does Sublime use. – Ferit May 22 '17 at 18:34
  • @S.Li Which version of python shows up when you use `python` at the command line/terminal? Which version shows up when you [run a script](https://stackoverflow.com/questions/1093322/how-do-i-check-what-version-of-python-is-running-my-script#1093331)? Right now I have those configured to two different pythons, so please double check. I can confirm that the interpreter I'm using, 3.4.2, works as it should and unlike your examples. – Aaron3468 May 22 '17 at 18:34
  • @Saibot Sublime Text runs the code itself. How do I check the binary? – S. Li May 22 '17 at 18:36
  • @Aaron3468 If I use the terminal, then it's definitely 3.6.1. But when I use Sublime Text, I'm not sure. I think it's not 3.6.1, otherwise 3/2 should give me 1.5. But I don't know how to set up or switch version. – S. Li May 22 '17 at 18:39
  • @S.Li I checked it, your sublime uses Python 2. Now I'm trying to find how to make it use Python 3. Hold on : ) – Ferit May 22 '17 at 18:42
  • @Saibot Thanks. I checked it myself. It shows 2.7.10. That's the problem. I just don't know how to change the Python version in Sublime Text. – S. Li May 22 '17 at 18:45
  • @S.Li The reason running a python file ran in 2.7 for me, while explicitly calling `python` ran 3.4.2, was that windows was still configured to `Open With` the wrong python interpreter by default on .py files. – Aaron3468 May 22 '17 at 18:46
  • @Aaron3468 I checked it out. Somehow Sublime Text still uses the 2.7.10 version. But I already installed 3.6.1 on my Mac. – S. Li May 22 '17 at 18:49
  • @Saibot Um... after I typed this `{ "cmd": ["python3", "-u", "$file"], "file_regex": "^[ ]File \"(...?)\", line ([0-9]*)", "selector": "source.python" }`, then what should I do. I tried to just save it. But it didn't work. – S. Li May 22 '17 at 19:01
  • Change your build system option from `tools > build system > $new_build_systems_name_dunno_whatever_name_you_gave_it$`. Then press build button. – Ferit May 22 '17 at 19:03
  • You have to use that configuration snippet to define a new build system. When you click `new build system` button under `tools > build system` a text file opens and you should paste that snippet into that file. Filename is untitled by default, change it to smth like python3.sublime-build for convenience. After that there will be an option in build system menu for python3. – Ferit May 22 '17 at 19:11
  • @Saibot um... There's still something wrong. I updated the question with the issue. – S. Li May 22 '17 at 19:26
  • @S.Li Please confirm that `python3` command works from terminal. And please post your build system file you just declared for Sublime, maybe you did a typo. – Ferit May 22 '17 at 19:28
  • @Saibot I posted the screenshot. – S. Li May 22 '17 at 19:38
  • @S.Li It looks like you applied it correctly(last screenshot builds with new system I guess). And it looks like you have another issue. Seemingly, somehow Sublime can't reach `python3` binary. – Ferit May 22 '17 at 19:48
  • @Saibot I also think so. I'll just use this for now. Thanks for the help anyway! – S. Li May 22 '17 at 19:54
  • @S.Li if I understood correctly, doing `python` from your terminal launches Python3, right? Then do `which python` from terminal to find its path, and use that path in place of the `"python3"` in the first part of the build system config. – Adam Smith May 22 '17 at 19:57
  • @AdamSmith Which version of Python the terminal uses actually depends on me. I added a screenshot, if you could check out. – S. Li May 22 '17 at 20:11
  • @S.Li that's normal on *nix-like systems (like MacOS). Lots of things internally expect `python` to resolve to Python2. Change my instructions above to check `which python3`, then use that path. – Adam Smith May 22 '17 at 20:13
  • @Saibot Hi, I just changed the path. It finally worked! – S. Li May 22 '17 at 20:18
  • @S.Li no problem! No idea why it didn't work just with `python3`. My best guess is something tech supporty ("Have you tried turning it off and back on again?") but who cares? :) – Adam Smith May 22 '17 at 20:19
  • @S.Li So yeah, there is a path configuration problem in your system, so `python3` doesn't work but when you entered full path of python3 it worked. – Ferit May 23 '17 at 07:19
  • @Saibot Right. Because Python3 wasn't installed in that default path I think. – S. Li May 23 '17 at 07:22
  • @S.Li Yep, probably. – Ferit May 23 '17 at 07:22