崇高的 text3 和 virtualenvs [英] Sublime text3 and virtualenvs

查看:46
本文介绍了崇高的 text3 和 virtualenvs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有不同的 virtualenv(用 virtualenwrapper 制作),我希望能够指定使用哪个 virtualenv每个项目.

I have different virtualenv's (made with virtualenwrapper) and I'd like to be able to specify which virtualenv to use with each project.

由于我使用 SublimeREPL 插件进行自定义构建,我该如何指定使用哪个 Python 安装来构建我的项目?

Since I'm using the SublimeREPL plugin for custom builds, how can I specify which python installation to build my project with?

例如,当我在项目 A 上工作时,我想用 venvA 的 python 运行脚本,而当我在项目 B 上工作时,我想用 venvB(使用不同的构建脚本)运行.

For example, when I work on project A I want to run scripts with venvA's python, and when I work on project B I want to run things with venvB (using a different build script).

推荐答案

希望这符合您的想象.我试图简化我的解决方案并删除一些您可能不需要的东西.

Hopefully this is along the lines you are imagining. I attempted to simplify my solution and remove some things you likely do not need.

这种方法的优点是:

  • 按一下按钮即可启动带有正确解释器的 SublimeREPL 根据需要在其中运行文件.
  • 设置解释器后,在项目之间切换时无需更改或额外步骤.
  • 可以轻松扩展以自动获取项目特定的环境变量、所需的工作目录、运行测试、打开 Django shell 等.
  • Single button press to launch a SublimeREPL with correct interpreter and run a file in it if desired.
  • After setting the interpreter, no changes or extra steps are necessary when switching between projects.
  • Can be easily extended to automatically pick up project specific environment variables, desired working directories, run tests, open a Django shell, etc.

如果您有任何问题,或者我完全没有注意到您想要做什么,请告诉我.

Let me know if you have any questions, or if I totally missed the mark on what you're looking to do.

  1. 打开我们的项目文件进行

  1. Open our project file for editing:

 Project -> Edit Project

  • 向项目设置中添加一个指向所需 virtualenv 的新键:

  • Add a new key to the project's settings that points to the desired virtualenv:

     "settings": {
         "python_interpreter": "/home/user/.virtualenvs/example/bin/python"
     }
    

  • "python_interpreter" 项目设置键也被 Anaconda<等插件使用/a>.

    A "python_interpreter" project settings key is also used by plugins like Anaconda.

    1. 浏览到 Sublime Text 的 Packages 目录:

    Preferences -> Browse Packages...
    

  • 为我们的插件创建一个新的 python 文件,例如:project_venv_repls.py

    将以下 python 代码复制到这个新文件中:

    Copy the following python code into this new file:

    import sublime_plugin
    
    
    class ProjectVenvReplCommand(sublime_plugin.TextCommand):
        """
        Starts a SublimeREPL, attempting to use project's specified
        python interpreter.
        """
    
        def run(self, edit, open_file='$file'):
            """Called on project_venv_repl command"""
            cmd_list = [self.get_project_interpreter(), '-i', '-u']
    
            if open_file:
                cmd_list.append(open_file)
    
            self.repl_open(cmd_list=cmd_list)
    
        def get_project_interpreter(self):
            """Return the project's specified python interpreter, if any"""
            settings = self.view.settings()
            return settings.get('python_interpreter', '/usr/bin/python')
    
        def repl_open(self, cmd_list):
            """Open a SublimeREPL using provided commands"""
            self.view.window().run_command(
                'repl_open', {
                    'encoding': 'utf8',
                    'type': 'subprocess',
                    'cmd': cmd_list,
                    'cwd': '$file_path',
                    'syntax': 'Packages/Python/Python.tmLanguage'
                }
            )
    

  • 设置热键

    1. 打开用户键绑定文件:

    1. Open user keybind file:

     Preferences -> Key Bindings - User
    

  • 添加几个键绑定以使用插件.一些例子:

  • Add a few keybinds to make use of the plugin. Some examples:

        // Runs currently open file in repl
        {
            "keys": ["f5"],
            "command": "project_venv_repl"
        },
        // Runs repl without any file
        {
            "keys": ["f6"],
            "command": "project_venv_repl",
            "args": {
                "open_file": null
            }
        },
        // Runs a specific file in repl, change main.py to desired file
        {
            "keys": ["f7"],
            "command": "project_venv_repl",
            "args": {
                "open_file": "/home/user/example/main.py"
            }
        }
    

    这篇关于崇高的 text3 和 virtualenvs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆