I've been following the steps to a tutorial and (1) created a new directory for my project, (2) run C:\Users\JohnSmith\AppData\Local\Programs\Python\Python38\python -m venv .venv in that directory to create the virtual environment, (3) run .venv\Scripts\activate 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
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.
{
"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.
I would highly reccomend watching this video which does a good job of explaining the whole concept.