4

For starters, here is my dev environment:

  • Windows 7 (although I have the same issue on another machine that is Windows 10)
  • Python 3.6
  • Git Bash
  • Sublime Text 3 (version 3.1.1, Build 3176)
  • SublimeREPL

In Git Bash, I created a new virtual environment:

$ mkdir ~/.venv
$ cd ~/.venv
$ python -m venv test-env

To activate that virtual environment, I use:

$ source ~/.venv/test-env/Scripts/activate

NOTE: I had to modify the activate script (NOT activate.bat) to get the venv to activate properly. Specifically, I changed line 40 which looked something like:

VIRTUAL_ENV="C:\Users\my_user_name\.venv\test-env"

to

VIRTUAL_ENV="/c/Users/my_user_name/.venv/test-env"

Now, when I am in the test-env virtual environment (as evidenced by the "(test-env)" text in Git Bash), I can do the usual stuff like

(test-env)
$ pip install numpy

I also have the SublimeREPL package installed in Sublime Text 3. I setup a new build system (SublimeREPL-python.sublime-build) that looks like:

{
    "target": "run_existing_window_command", 
    "id": "repl_python_run",
    "file": "config/Python/Main.sublime-menu"
}

Now, suppose I have a script

# test.py
import numpy as np


print('numpy was imported without error')

I can type Ctrl+Shift+B, then start typing 'repl', which autoselects the SublimeREPL-python build, then hit Enter. The SublimeREPL appears, but generates an error:

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    import numpy as numpy
ModuleNotFoundError: No module named 'numpy'
>>> 

SublimeREPL was called without using my virtual environment, which causes it to throw the error because numpy wasn't installed in my global python environment.

How can I run my Python script from Sublime Text 3 using SublimeREPL and accessing my virtual environment that was created using venv?

FWIW, I already tried creating a Sublime Project for this code and adding the following to the .sublime-project file:

"build_systems":
[
    {
        "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
        "name": "test-env",
        "selector": "source.python",
        "shell_cmd": "\"C:\\Users\\my_user_name\\.venv\\test-env\\Scripts\\python\" -u \"$file\""
    }
]

This allowed me to type Ctrl+Shift+B, then "test-env", then Enter (to build with the test-env build system I just created), which worked as expected and ran without error. However, it does not use SublimeREPL, which I'd like so that I can debug my code (which is more complicated that the simple test script I posted above!) and explore the variables in the REPL rather than just running code in the console.

meicholtz
  • 76
  • 1
  • 6

1 Answers1

1

I know it's not an answer, but a partial solution that might help anyone else on same situation as I am. Also because SublimeREPL support is almost nothing.

Note: This solution requires to modify Main.sublime-menu for every environment and assumes SublimeREPL runs interactively already (adding the "-i" for execution).

  1. Browse packages and open SublimeREPL/config/python/Main-sublime-menu.
  2. Search for line with "id": "repl_python_run".
  3. Duplicate the nearest content inside the curly brackes.

Basically pasting after the same end bracket the following (note the "-i" option on "cmd" to run interactively):

{"command": "repl_open",
"caption": "Python - RUN current file",
"id": "repl_python_run",
"mnemonic": "R",
"args": {
   "type": "subprocess",
   "encoding": "utf8",
   "cmd": ["python", "-u", "-i", "$file_basename"],
   "cwd": "$file_path",
   "syntax": "Packages/Python/Python.tmLanguage",
   "external_id": "python",
   "extend_env": {"PYTHONIOENCODING": "utf-8"}
   }
},
  1. Change the id repl_python_run to whatever you like, e.g., repl_my_first_env_python.
  2. Change python commmand from "cmd": ["python", "-u", "-i", "$file_basename"] to use wherever your python's virtual environment executable is, e.g., /home/me/.virtualenvs/my_first_env/bin/python (linux example). Save the file.
  3. Edit project to include inside the square brackets from "build_systems" the following:

(If you have no project, create a new Build System and paste next block of code)

{
    "name": "my first env",
    "target": "run_existing_window_command",
    "id": "repl_my_first_env_python",
    "file": "config/Python/Main.sublime-menu"
},
  1. Finally Save.

You'll see when Ctrl+Shift+B you'll see my first env as an option.

Traxidus Wolf
  • 808
  • 1
  • 9
  • 18