简体   繁体   English

崇高的 text3 和 virtualenvs

[英]Sublime text3 and virtualenvs

I have different virtualenv 's (made with virtualenwrapper ) and I'd like to be able to specify which virtualenv to use with each project.我有不同的virtualenv (用virtualenwrapper ),我希望能够指定每个项目使用哪个virtualenv

Since I'm using the SublimeREPL plugin for custom builds, how can I specify which python installation to build my project with?由于我使用SublimeREPL插件进行自定义构建,如何指定使用哪个 python 安装来构建我的项目?

For example, when I work on project AI want to run scripts with venvA's python, and when I work on project BI want to run things with venvB (using a different build script).例如,当我在项目 AI 上工作时想要使用 venvA 的 python 运行脚本,而当我在项目 BI 上工作时想要使用 venvB(使用不同的构建脚本)运行。

Hopefully this is along the lines you are imagining.希望这符合您的想象。 I attempted to simplify my solution and remove some things you likely do not need.我试图简化我的解决方案并删除一些您可能不需要的东西。

The advantages of this method are:这种方法的优点是:

  • Single button press to launch a SublimeREPL with correct interpreter and run a file in it if desired.按下单个按钮以启动带有正确解释器的 SublimeREPL,在需要时在其中运行文件。
  • 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.可以轻松扩展以自动获取项目特定的环境变量、所需的工作目录、运行测试、打开 Django shell 等。

Let me know if you have any questions, or if I totally missed the mark on what you're looking to do.如果您有任何问题,或者我完全没有注意到您想要做的事情,请告诉我。

Set Project's Python Interpreter设置项目的 Python 解释器

  1. Open our project file for editing:打开我们的项目文件进行编辑:

     Project -> Edit Project
  2. Add a new key to the project's settings that points to the desired virtualenv:向项目设置中添加一个指向所需 virtualenv 的新键:

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

A "python_interpreter" project settings key is also used by plugins like Anaconda . Anaconda 等插件也使用"python_interpreter"项目设置键。

Create plugin to grab this setting and launch a SublimeREPL创建插件以获取此设置并启动 SublimeREPL

  1. Browse to Sublime Text's Packages directory:浏览到 Sublime Text 的Packages目录:

     Preferences -> Browse Packages...
  2. Create a new python file for our plugin, something like: project_venv_repls.py为我们的插件创建一个新的 python 文件,例如: project_venv_repls.py

  3. Copy the following python code into this new file:将以下 python 代码复制到这个新文件中:

     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' } )

Set Hotkeys设置热键

  1. Open user keybind file:打开用户密钥绑定文件:

     Preferences -> Key Bindings - User
  2. 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"
        }
    }

There is a sublime text3 package, named Virtualenv , allowing you to build using the Python from your virtualenv.有一个名为Virtualenv的 sublime text3 包,允许您使用来自 virtualenv 的 Python 进行构建。

It supports any versions of Python in your virtualenv, and works very well for me (MacOS).它支持您的 virtualenv 中的任何版本的 Python,并且对我(MacOS)非常有效。

To install it, we just command + Shift + P to call out pacakge control (install it if you don't have it yet), then type install .要安装它,我们只需命令+ Shift + P调用 pacakge 控件(如果您还没有它, install它),然后键入install Next type virtualenv , when you see it appears click return to install it.接下来输入virtualenv ,当你看到它出现时点击返回来安装它。

After installing it, select Tools --> Build System --> Python + Virtualenv .安装后,选择Tools --> Build System --> Python + Virtualenv Then you can use command + B to execute your Python projects.然后你可以使用command + B来执行你的 Python 项目。

Click Here to check further information.单击此处查看更多信息。

I have an alternative.我有一个替代方案。 Just creating a new 'Build System' which runs as if in the virtual environment.只需创建一个新的“构建系统”,就像在虚拟环境中一样运行。 Here we call it 'my_python'.这里我们称它为“my_python”。 The aim is to use this build system to run my script directly without leaving sublime.目的是使用这个构建系统直接运行我的脚本而不离开 sublime。 You need to:你需要:

  1. First, preferences->Browse Packages .首先, preferences->Browse Packages This will open a folder under which lies setting files.这将打开一个文件夹,其中包含设置文件。 Go inside dir User and create a new file named my_python.sublime-build (composed of the build system name followed by .sublime_build . After doing this, go to Tools -> Build System and you will see a new option my_python .进入 dir User并创建一个名为my_python.sublime-build的新文件(由构建系统名称后跟.sublime_build 。执行此操作后,转到Tools -> Build System ,您将看到一个新选项my_python
  2. Add the following JSON settings to that file.将以下 JSON 设置添加到该文件。

     { "shell_cmd": "/Users/Ted/bsd/vector/.v_venv/bin/python -u \\"$file\\"", "file_regex": "^[ ]*File \\"(...*?)\\", line ([0-9]*)", "selector": "source.python" }

    Inside shell_cmd , change /Users/Ted/bsd/vector/.v_venv/bin/python to the path of your python virtual environment.shell_cmd ,将/Users/Ted/bsd/vector/.v_venv/bin/python更改为 python 虚拟环境的路径。

  3. Then just use the short key to build your script.然后只需使用快捷键来构建您的脚本。

When you want to change your virtual environment, just copy its path to the setting file and all done.当您想更改虚拟环境时,只需将其路径复制到设置文件即可。 Maybe the approach seems containing a lots of work, but it works well and convenient.也许这种方法看起来包含很多工作,但它运行良好且方便。

You're looking for custom build systems.您正在寻找自定义构建系统。

From the menu bar, click Tools -> Build Systems -> New Build System...从菜单栏中,单击Tools -> Build Systems -> New Build System...

Fill out the template it gives and save it under any filename ending in .sublime-build to your User folder.填写它提供的模板并将其保存在以.sublime-build结尾的任何文件.sublime-build到您的User文件夹。

Here is the documentation for making custom build systems:以下是制作自定义构建系统的文档:

https://www.sublimetext.com/docs/3/build_systems.html https://www.sublimetext.com/docs/3/build_systems.html

I recommend making a custom build system for python scripts, then add variants for each virtual env you want.我建议为 python 脚本制作一个自定义构建系统,然后为您想要的每个虚拟环境添加变体。 (see variants https://www.sublimetext.com/docs/3/build_systems.html#option-variants ) (见变体https://www.sublimetext.com/docs/3/build_systems.html#option-variants

One you make a build system, you can switch them from您制作了一个构建系统,您可以从中切换它们

Tools -> Build Systems

(if not auto detected) and use the Command Palette (default ctrl + shift p) to switch between variants. (如果没有自动检测到)并使用命令面板(默认 ctrl + shift p)在变体之间切换。

The only "gotcha" is the "cmd" parameter to describe what command to run.唯一的“问题”是"cmd"参数,用于描述要运行的命令。 By default it takes an array of strings to run as a command, but you can use "shell_cmd" instead to just use a string of how you would run it via command line.默认情况下,它需要一个字符串数组作为命令运行,但您可以使用"shell_cmd"来代替仅使用您将如何通过命令行运行它的字符串。

I'm a super newb to Python, pipenv, and especially Sublime Text 3. Couldn't find a simple solution to get me started working with pipenv virtual environment and sublime on Windows 10.我是 Python、pipenv,尤其是 Sublime Text 3 的超级新手。找不到让我开始在 Windows 10 上使用 pipenv 虚拟环境和 sublime 的简单解决方案。

This works really well for someone who doesn't have much systems experience.这对于没有太多系统经验的人来说非常有效。

My setup:我的设置:

 Windows 10 Pro
 Python 3.9
 Sublime Text 3
 Dedicated directory for Python virtual environment 'C:Users\username\VirtualEnvs`
 
  1. create a virtual environment using pipenv in a dedicated directory: pipenv install BestPythonPackage在专用目录中使用 pipenv 创建虚拟环境: pipenv install BestPythonPackage
  2. from the dedicated directory run: pipenv shell从专用目录运行: pipenv shell
  3. get the virtual environment path;获取虚拟环境路径; while in the shell run: pipenv --venv在 shell 中运行时: pipenv --venv
  4. open folder in sublime在崇高中打开文件夹
  5. create new project: Project > Save Project As > "New Project Name"创建新项目: Project > Save Project As > "New Project Name"
  6. select "New Project Name".sublime-project from file list in left column从左栏中的文件列表中选择"New Project Name".sublime-project
  7. replace contents of that file with the below and save.用以下内容替换该文件的内容并保存。 The path is what you found from running 'pipenv --venv'该路径是您从运行“pipenv --venv”中找到的
{
    "folders":
    [
        {
            "path": "C:\\Users\\username\\.virtualenvs\\PythonEnvs-uKL8m4_g",           
        },
    ],
    
    "build_systems": [
      {
      "name": "PYTHON_PIPENV",
      "cmd": ["python", "$file"],     
      }
    ], 
}
  1. Observe the sublime folder name for your dedicated folder has changed to the name of the virtual environment.观察专用文件夹的崇高文件夹名称已更改为虚拟环境的名称。

  2. Create a 'hello.py' program from the folder.从文件夹创建一个“hello.py”程序。

  3. build the program with Tools > Build System > "PYTHON_PIPENV" build system now in the list.现在使用列表中的Tools > Build System > "PYTHON_PIPENV"构建系统构建程序。

Of course, the caveat here is you'd need to do this for any new virtual environment.当然,这里的警告是您需要为任何新的虚拟环境执行此操作。 Some of the other solutions provided will dynamically update.提供的一些其他解决方案将动态更新。 Those were/are over my head at the moment.那些是/现在在我的头上。 This is a simple solution.这是一个简单的解决方案。

'folders' section tells sublime what directory to look at 'build_systems' gives a name for the new config, and tells sublime to run 'python' on whatever file you have open. 'folders' 部分告诉 sublime 要查看的目录 'build_systems' 为新配置命名,并告诉 sublime 在打开的任何文件上运行 'python'。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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