How to use SublimeText within a venv?

Question:

I’ve been following the steps to a tutorial and (1) created a new directory for my project, (2) run C:UsersJohnSmithAppDataLocalProgramsPythonPython38python -m venv .venv in that directory to create the virtual environment, (3) run .venvScriptsactivate to activate it, (4) run to install python -m pip install flask which should only be available in this environment, (5) flask –version confirms that it is installed in (.venv), here is the output from this last command:

Python 3.9.0

Flask 1.1.2

Werkzeug 1.0.1

The issue is when I try to reference flask in my code it returns the following: ModuleNotFoundError: No module named ‘flask’.

I saw some posts about creating a new Build System but I have no clue how to do that. Anyone else set up something similar? Please don’t recommend some other virtual environment, since I’m committed to following this tutorial and venv is the tool he uses which comes included in Python 3.9.

The first bit of code in the file ..app__init__.py has the following code that gives the above error (first line) when I hit CTRL+b:

from flask import Flask

app = Flask(__name__)

from app import routes
Asked By: Alex In Paris

||

Answers:

Virtual Environment is an independent and isolated installation of the python interpreter into which you have installed flask. Your sublime text is still probably running your original python interpreter(not the virtual env one). So you need to tell sublime which interpreter you want to use. It is probably best to learn about build systems if you want to make this process of switching between different environments quick and painless.


I tried to manually edit the python build but it was not successful and it would be a pain to manually change it every time you wanted to swtich to a new python env. So we go with build systems.


  1. Select Tools >> New build system in sublime text. This will open a new build system config file. In that file delete everything and paste this:
{
    "cmd": ["PATH TO YOUR DESIRED PYTHON INTERPRETER","-u", "$file"],
    "selector": "source.python",
    "file_regex": "^\s*File "(...*?)", line ([0-9]*)"
}

To work out the path to the desired python interpreter, in the terminal, go to your project directory, make sure it is activated, then use which python3 command.

  1. Save the file and then run your program with the new build system. It will appear in the build system tab in tools.

I would highly reccomend watching this video which does a good job of explaining the whole concept.

Answered By: Harsha Limaye
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.