Using virtualenv with Sublime Text 3 and SublimeREPL
Asked Answered
O

2

13

I'm trying to setup ST3 to work with Python's virtualenv, running on Windows 8.1. I usually use SublimeREPL with my global Python install to run files. Now that I'm using venvs, though, I'm having trouble getting anything to run. Here's what I've been trying:

I have a parent directory with a folder virtualenvs, then one scripts for my .py files that I muck around with. I usually just navigate to \virtualenvs\venv\scripts\activate and do my work using the python interpreter, but I'd like to be able to build the files without needing to go through command line stuff, using ST3 and SublimeREPL.

I made a build system that looks like this:

{
    "shell_cmd": ["\code\virtualenvs\venv\scripts\python.exe", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",
}

But I'm not even sure if this is what I need to do to use SublimeREPL, as I've never had to worry about Tools > Build Systems before, just Tools > SublimeREPL > Python > Python - RUN current file.

How can I change the build system or customize SublimeREPL's RUN? For a test case, I have the requests module installed in the venv but not in my global python install. Importing it from command line works fine but a .py file with import requests returns an ImportError.

Odom answered 17/5, 2015 at 21:4 Comment(0)
B
4

The ST3 build system and the SublimeREPL provide two different pieces of functionality. The build system is something you would typically use when working with a compiled language like C. It allows you to define how you want to build your program from the source files.

You can also use the build system with Python. In this case, it would compile for you the file which is currently open.

The SublimeREPL on the other hand, allows you to run a terminal session inside Sublime and also (among other things) evaluate the files within the context of that session. I would suggest you take a look at the documentation for SublimeREPL and Python:

Both stock Python and Execnet integrations support virtualenv. Various ways to work with Python, including PDB and IPython, are supported.

For virtualenv created environments to be discoverable by SublimeREPL they should be created or symlinked in one of the following:

  • ~/.virtualenvs default for virtualenvwrapper
  • ~/.venvs default for venv

Alternatively, more paths can be added to “python_virtualenv_paths” in the SublimeREPL configuration file.

Beeck answered 17/5, 2015 at 22:33 Comment(0)
S
5
  1. install 'virtualenv' package in sublime editor
  2. Then goto to build system and set it as 'python + virtualenv' enter image description here 3.Now go to the sublime project file and add path to the virtualenv "virtualenv":"D:/my_projects/scrapping_env", enter image description here
  3. To add Test build system add the following in the .sublime-project settings "build_systems": [ { "name": "Test", "shell_cmd": "D:/my_projects/scrapping_env/Scripts/python py.test" }, ],

  4. Select Test build sytem from the tools as shown enter image description here

Sophronia answered 24/9, 2016 at 10:3 Comment(1)
could you help - I tried your suggestions, I kept having error python: realpath couldn't resolve "/Library/Frameworks/Python.framework/Versions/3.6/bin/python" I also tried to set package settings > virtualenv > user and specify directory of my local folder virtualenv (named venv3) but keep having error that cannot locate the interpreter. Could you help so I avoid ask duplicate questions?Rivalry
B
4

The ST3 build system and the SublimeREPL provide two different pieces of functionality. The build system is something you would typically use when working with a compiled language like C. It allows you to define how you want to build your program from the source files.

You can also use the build system with Python. In this case, it would compile for you the file which is currently open.

The SublimeREPL on the other hand, allows you to run a terminal session inside Sublime and also (among other things) evaluate the files within the context of that session. I would suggest you take a look at the documentation for SublimeREPL and Python:

Both stock Python and Execnet integrations support virtualenv. Various ways to work with Python, including PDB and IPython, are supported.

For virtualenv created environments to be discoverable by SublimeREPL they should be created or symlinked in one of the following:

  • ~/.virtualenvs default for virtualenvwrapper
  • ~/.venvs default for venv

Alternatively, more paths can be added to “python_virtualenv_paths” in the SublimeREPL configuration file.

Beeck answered 17/5, 2015 at 22:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.