简体   繁体   中英

MacOS: different treatment of IDEs and terminal when running code?

I am having a problem with a python IDE (Sublime) on macos when trying to use ktinter:

I set up a clean mac and installed Python 3.7 via the normal installer (with the box ticked to install tkinter)

The problem I am having now is this: When I write code in Sublime and try to run it within the IDE (Sublime -> Tools -> Build) I get an error message.

Code

import tkinter as tk
tk._test()

Output

Traceback (most recent call last):
  File "/Users/christiankoch/Desktop/sys.py", line 1, in <module>
    import tkinter as tk
ImportError: No module named tkinter
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u "/Users/christiankoch/Desktop/sys.py"]
[dir: /Users/christiankoch/Desktop]
[path: /Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin] 

However, when I just save the file and run it in the terminal everything works fine and I see the test window.

In the terminal

python3 test.py

So ultimately I am confused about different outcomes of the two approaches. When I use Sublime on Windows it works perfectly fine; and even using pyCharm on macos works for tkinter. So I am confused what the problem is.

When you run file.py from the console, the executable as specified in the environment variables is used by default. In sublime you probably using a build system, that points to another environment/ executable. You should have a look at https://youtu.be/xqcTfplzr7c And follow the instructions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM