sublime text3配置Python3.5开发环境


最近开始捣鼓深度学习,前天从tensorflow的大坑中爬了粗来,在台式机上完美配置gpu版本的tensorflow,于是就有了上一期博客:Windows10+tensorflow(gpu)。

环境搭好之后,面临的问题就是选一款顺手的IDE。说实话,之前一直在使用eclipse写点Python和java web。但是它启动速度真的很慢,运行起来,我的小破本(i5五代 6G内存,2G显卡)真是呼呼直响。听说装Linux系统比较好,不吃硬件,可以解决笔记本上岁数硬件跟不上的问题,苦于电脑里资料太多,格盘再装ubuntu系统更是不现实。所以最近瞄上了在用风靡全球的sublime,于是乎就选它安装一个Python环境来开发吧。

本篇适合原来使用其他IDE开发python,现在改用sublime的人群,以及刚开始学习python并决定使用sublime的人群

——前言结束

——————————————————————————————————————————————————————————————————————————

言归正传,看步骤:

第一步:安装Python

从Python官网可以下载Python:https://www.python.org/downloads/

在网上查了很多sublime text3配置Python3.5开发环境的博客文章,大部分第一步都是安装sublime text。由于我已经用Eclipse开发Python一段时间,近期才后装的sublime,因此我还是按照自己的步骤来描述吧。

该步可以分为两种情况:

1.Python安装路径已添加在环境变量中;

对于使用其他ide开发Python有一段时间,环境变量肯定早就配过了。该步骤可以忽略。

2.Python没有添加环境变量。

 可用sublime配置pytho多版本开发环境。

第二步:安装sublime text

官网下载地址:https://www.sublimetext.com/

1.安装sublime

安装结尾时,注意勾选Add to explorer context menu,这样Sublime Text可以被添加到右键中,在右键单击文件时,可以直接使用Sublime Text打开。

2.注册sublime

在help——>enter license  复制一下代码

—– BEGIN LICENSE —–
TwitterInc
200 User License
EA7E-890007
1D77F72E 390CDD93 4DCBA022 FAF60790
61AA12C0 A37081C5 D0316412 4584D136
94D7F7D4 95BC8C1C 527DA828 560BB037
D1EDDD8C AE7B379F 50C9D69D B35179EF
2FE898C4 8E4277A8 555CE714 E1FB0E43
D5D52613 C3D12E98 BC49967F 7652EED2
9D2D2E61 67610860 6D338B72 5CF95C69
E36B85CC 84991F19 7575D828 470A92AB
—— END LICENSE ——

第三步骤:sublime text 配置

1.安装python环境

(1)如果是第一步中第1种情况的人群,则可以在直接在sublime 菜单栏中选择tools——>build system——>python(如下图所示),既可以开始开发。

(2)如果是第一步中第2种情况的人群,则需要添加开发环境。

新建开发环境:tools——>build system——>New build system.....


把如下代码复制到弹出的窗口中,注意***号部分是自己python安装目录

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

注意标红部分为自己的pytho安装目录。

编辑完之后保存,

保存目录就放在默认路径下,起个自己顺眼的名字。比如:Python2


现在打开tools——>build system后就能看到自己添加的python环境。

检验结果

输入如图所示一段简单代码,然后保存,一定要保存保存保存,保存时填写文件后缀“.py”,按快捷键ctrl+B,运行。结果如下图


(3)同理,可以再添加另外一个版本的python.

当然第一步中两种情况的人群都可以添加另一个版本的python(注意该版本python未添加环境变量)。

2.安装插件:

(1)安装package control

sublime text只是文本编辑器,打开极快,但是需要添加一些package来完善编译环境。

使用ctrl+~(数字1旁边的按键),调出控制台输入一下代码:

import urllib.request,os; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); open(os.path.join(ipp, pf), 'wb').write(urllib.request.urlopen( 'http://sublime.wbond.net/' + pf.replace(' ','%20')).read())

然后回车,等一会就可以安装完成,完成后在preferences中可以看到。这个时候我们就可以通过它来安装插件。

(2)利用package control安装插件

本次只介绍安装sublimerepl
sublime text只是文本编辑器并没有提供python的input()的输入窗口,当我们需要输入数据时,需要使用sublime repl插件

打开package control 输入install package ,选择对应项目回车后,在出现的框里输入sublime text

验证结果

可以在出现的repl窗口中给a赋值,给b赋值,最后的出结果

如果每次调出repl界面都需要操作好几步的话确实很麻烦,现在我们来设置repl的快捷键。

(3)设置python3的快捷键

打开 preferences——>key bindings ,在弹出窗口输入一下代码:

    {  
      
    "keys":["f5"],  
    "caption": "SublimeREPL: Python - RUN current file",  
    "command": "run_existing_window_command", "args": {"id": "repl_python_run",  
    "file": "config/Python/Main.sublime-menu"}  
      
    } 

 

最后别忘了保存,ctrl+s或者菜单栏file——>save


好了,到这里sublime text3安装python环境就配置完了。现在写好的python代码可以按f5执行程序了,执行之前务必保存。


发布了2 篇原创文章 · 获赞 1 · 访问量 6946
展开阅读全文

CentOS sublime text build error

05-29

build system 文件内容: { "cmd": ["make", "linux"], "working_dir": "/home/cyb/lua-5.3.4/src", "encoding": "utf-8" } build 错误信息: Running make linux Exception in thread Thread-4: Traceback (most recent call last): File "./python3.3/threading.py", line 901, in _bootstrap_inner File "./python3.3/threading.py", line 858, in run File "/opt/sublime_text/Packages/Default.sublime-package/exec.py", line 152, in read_fileno decoder_cls = codecs.getincrementaldecoder(self.listener.encoding) AttributeError: 'NoneType' object has no attribute 'encoding' 如果我把build system里的命令写错,比如把make写成其他的不存在的命令,在sublime的build result窗口会显示我的命令有错误,但是如果命令对了,成功进入make的环节,就会报错~ code in exec.py ``` import collections import functools import html import os import subprocess import sys import threading import time import codecs import signal import sublime import sublime_plugin class ProcessListener(object): def on_data(self, proc, data): pass def on_finished(self, proc): pass class AsyncProcess(object): """ Encapsulates subprocess.Popen, forwarding stdout to a supplied ProcessListener (on a separate thread) """ def __init__(self, cmd, shell_cmd, env, listener, path="", shell=False): """ "path" and "shell" are options in build systems """ if not shell_cmd and not cmd: raise ValueError("shell_cmd or cmd is required") if shell_cmd and not isinstance(shell_cmd, str): raise ValueError("shell_cmd must be a string") self.listener = listener self.killed = False self.start_time = time.time() # Hide the console window on Windows startupinfo = None if os.name == "nt": startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW # Set temporary PATH to locate executable in cmd if path: old_path = os.environ["PATH"] # The user decides in the build system whether he wants to append $PATH # or tuck it at the front: "$PATH;C:\\new\\path", "C:\\new\\path;$PATH" os.environ["PATH"] = os.path.expandvars(path) proc_env = os.environ.copy() proc_env.update(env) for k, v in proc_env.items(): proc_env[k] = os.path.expandvars(v) if sys.platform == "win32": preexec_fn = None else: preexec_fn = os.setsid if shell_cmd and sys.platform == "win32": # Use shell=True on Windows, so shell_cmd is passed through with the correct escaping self.proc = subprocess.Popen( shell_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, startupinfo=startupinfo, env=proc_env, shell=True) elif shell_cmd and sys.platform == "darwin": # Use a login shell on OSX, otherwise the users expected env vars won't be setup self.proc = subprocess.Popen( ["/usr/bin/env", "bash", "-l", "-c", shell_cmd], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, startupinfo=startupinfo, env=proc_env, preexec_fn=preexec_fn, shell=False) elif shell_cmd and sys.platform == "linux": # Explicitly use /bin/bash on Linux, to keep Linux and OSX as # similar as possible. A login shell is explicitly not used for # linux, as it's not required self.proc = subprocess.Popen( ["/usr/bin/env", "bash", "-c", shell_cmd], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, startupinfo=startupinfo, env=proc_env, preexec_fn=preexec_fn, shell=False) else: # Old style build system, just do what it asks self.proc = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, startupinfo=startupinfo, env=proc_env, preexec_fn=preexec_fn, shell=shell) if path: os.environ["PATH"] = old_path if self.proc.stdout: threading.Thread( target=self.read_fileno, args=(self.proc.stdout.fileno(), True) ).start() if self.proc.stderr: threading.Thread( target=self.read_fileno, args=(self.proc.stderr.fileno(), False) ).start() def kill(self): if not self.killed: self.killed = True if sys.platform == "win32": # terminate would not kill process opened by the shell cmd.exe, # it will only kill cmd.exe leaving the child running startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW subprocess.Popen( "taskkill /PID %d /T /F" % self.proc.pid, startupinfo=startupinfo) else: os.killpg(self.proc.pid, signal.SIGTERM) self.proc.terminate() self.listener = None def poll(self): return self.proc.poll() is None def exit_code(self): return self.proc.poll() def read_fileno(self, fileno, execute_finished): decoder_cls = codecs.getincrementaldecoder(self.listener.encoding) decoder = decoder_cls('replace') while True: data = decoder.decode(os.read(fileno, 2**16)) if len(data) > 0: if self.listener: self.listener.on_data(self, data) else: try: os.close(fileno) except OSError: pass if execute_finished and self.listener: self.listener.on_finished(self) break class ExecCommand(sublime_plugin.WindowCommand, ProcessListener): BLOCK_SIZE = 2**14 text_queue = collections.deque() text_queue_proc = None text_queue_lock = threading.Lock() proc = None errs_by_file = {} phantom_sets_by_buffer = {} show_errors_inline = True def run( self, cmd=None, shell_cmd=None, file_regex="", line_regex="", working_dir="", encoding="utf-8", env={}, quiet=False, kill=False, update_phantoms_only=False, hide_phantoms_only=False, word_wrap=True, syntax="Packages/Text/Plain text.tmLanguage", # Catches "path" and "shell" **kwargs): if update_phantoms_only: if self.show_errors_inline: self.update_phantoms() return if hide_phantoms_only: self.hide_phantoms() return # clear the text_queue with self.text_queue_lock: self.text_queue.clear() self.text_queue_proc = None if kill: if self.proc: self.proc.kill() self.proc = None self.append_string(None, "[Cancelled]") return if not hasattr(self, 'output_view'): # Try not to call get_output_panel until the regexes are assigned self.output_view = self.window.create_output_panel("exec") # Default the to the current files directory if no working directory was given if working_dir == "" and self.window.active_view() and self.window.active_view().file_name(): working_dir = os.path.dirname(self.window.active_view().file_name()) self.output_view.settings().set("result_file_regex", file_regex) self.output_view.settings().set("result_line_regex", line_regex) self.output_view.settings().set("result_base_dir", working_dir) self.output_view.settings().set("word_wrap", word_wrap) self.output_view.settings().set("line_numbers", False) self.output_view.settings().set("gutter", False) self.output_view.settings().set("scroll_past_end", False) self.output_view.assign_syntax(syntax) # Call create_output_panel a second time after assigning the above # settings, so that it'll be picked up as a result buffer self.window.create_output_panel("exec") self.encoding = encoding self.quiet = quiet self.proc = None if not self.quiet: if shell_cmd: print("Running " + shell_cmd) elif cmd: cmd_string = cmd if not isinstance(cmd, str): cmd_string = " ".join(cmd) print("Running " + cmd_string) sublime.status_message("Building") show_panel_on_build = sublime.load_settings("Preferences.sublime-settings").get("show_panel_on_build", True) if show_panel_on_build: self.window.run_command("show_panel", {"panel": "output.exec"}) self.hide_phantoms() self.show_errors_inline = sublime.load_settings("Preferences.sublime-settings").get("show_errors_inline", True) merged_env = env.copy() if self.window.active_view(): user_env = self.window.active_view().settings().get('build_env') if user_env: merged_env.update(user_env) # Change to the working dir, rather than spawning the process with it, # so that emitted working dir relative path names make sense if working_dir != "": os.chdir(working_dir) self.debug_text = "" if shell_cmd: self.debug_text += "[shell_cmd: " + shell_cmd + "]\n" else: self.debug_text += "[cmd: " + str(cmd) + "]\n" self.debug_text += "[dir: " + str(os.getcwd()) + "]\n" if "PATH" in merged_env: self.debug_text += "[path: " + str(merged_env["PATH"]) + "]" else: self.debug_text += "[path: " + str(os.environ["PATH"]) + "]" try: # Forward kwargs to AsyncProcess self.proc = AsyncProcess(cmd, shell_cmd, merged_env, self, **kwargs) with self.text_queue_lock: self.text_queue_proc = self.proc except Exception as e: self.append_string(None, str(e) + "\n") self.append_string(None, self.debug_text + "\n") if not self.quiet: self.append_string(None, "[Finished]") def is_enabled(self, kill=False, **kwargs): if kill: return (self.proc is not None) and self.proc.poll() else: return True def append_string(self, proc, str): was_empty = False with self.text_queue_lock: if proc != self.text_queue_proc and proc: # a second call to exec has been made before the first one # finished, ignore it instead of intermingling the output. proc.kill() return if len(self.text_queue) == 0: was_empty = True self.text_queue.append("") available = self.BLOCK_SIZE - len(self.text_queue[-1]) if len(str) < available: cur = self.text_queue.pop() self.text_queue.append(cur + str) else: self.text_queue.append(str) if was_empty: sublime.set_timeout(self.service_text_queue, 0) def service_text_queue(self): is_empty = False with self.text_queue_lock: if len(self.text_queue) == 0: # this can happen if a new build was started, which will clear # the text_queue return characters = self.text_queue.popleft() is_empty = (len(self.text_queue) == 0) self.output_view.run_command( 'append', {'characters': characters, 'force': True, 'scroll_to_end': True}) if self.show_errors_inline and characters.find('\n') >= 0: errs = self.output_view.find_all_results_with_text() errs_by_file = {} for file, line, column, text in errs: if file not in errs_by_file: errs_by_file[file] = [] errs_by_file[file].append((line, column, text)) self.errs_by_file = errs_by_file self.update_phantoms() if not is_empty: sublime.set_timeout(self.service_text_queue, 1) def finish(self, proc): if not self.quiet: elapsed = time.time() - proc.start_time exit_code = proc.exit_code() if exit_code == 0 or exit_code is None: self.append_string(proc, "[Finished in %.1fs]" % elapsed) else: self.append_string(proc, "[Finished in %.1fs with exit code %d]\n" % (elapsed, exit_code)) self.append_string(proc, self.debug_text) if proc != self.proc: return errs = self.output_view.find_all_results() if len(errs) == 0: sublime.status_message("Build finished") else: sublime.status_message("Build finished with %d errors" % len(errs)) def on_data(self, proc, data): # Normalize newlines, Sublime Text always uses a single \n separator # in memory. data = data.replace('\r\n', '\n').replace('\r', '\n') self.append_string(proc, data) def on_finished(self, proc): sublime.set_timeout(functools.partial(self.finish, proc), 0) def update_phantoms(self): stylesheet = ''' <style> div.error-arrow { border-top: 0.4rem solid transparent; border-left: 0.5rem solid color(var(--redish) blend(var(--background) 30%)); width: 0; height: 0; } div.error { padding: 0.4rem 0 0.4rem 0.7rem; margin: 0 0 0.2rem; border-radius: 0 0.2rem 0.2rem 0.2rem; } div.error span.message { padding-right: 0.7rem; } div.error a { text-decoration: inherit; padding: 0.35rem 0.7rem 0.45rem 0.8rem; position: relative; bottom: 0.05rem; border-radius: 0 0.2rem 0.2rem 0; font-weight: bold; } html.dark div.error a { background-color: #00000018; } html.light div.error a { background-color: #ffffff18; } </style> ''' for file, errs in self.errs_by_file.items(): view = self.window.find_open_file(file) if view: buffer_id = view.buffer_id() if buffer_id not in self.phantom_sets_by_buffer: phantom_set = sublime.PhantomSet(view, "exec") self.phantom_sets_by_buffer[buffer_id] = phantom_set else: phantom_set = self.phantom_sets_by_buffer[buffer_id] phantoms = [] for line, column, text in errs: pt = view.text_point(line - 1, column - 1) phantoms.append(sublime.Phantom( sublime.Region(pt, view.line(pt).b), ('<body id=inline-error>' + stylesheet + '<div class="error-arrow"></div><div class="error">' + '<span class="message">' + html.escape(text, quote=False) + '</span>' + '<a href=hide>' + chr(0x00D7) + '</a></div>' + '</body>'), sublime.LAYOUT_BELOW, on_navigate=self.on_phantom_navigate)) phantom_set.update(phantoms) def hide_phantoms(self): for file, errs in self.errs_by_file.items(): view = self.window.find_open_file(file) if view: view.erase_phantoms("exec") self.errs_by_file = {} self.phantom_sets_by_buffer = {} self.show_errors_inline = False def on_phantom_navigate(self, url): self.hide_phantoms() class ExecEventListener(sublime_plugin.EventListener): def on_load(self, view): w = view.window() if w is not None: w.run_command('exec', {'update_phantoms_only': True}) ``` 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览