Reputation: 37098
I've set up a virtualenv folder via python -m venv virtualenv
, alongside my backend
folder containing flask code.
However, when I run the flask code, it can't find the flask module. I installed it while the virtualenv was active, and if I try running it while the virtualenv is active, then it works correctly and finds flask. But if I deactivate the virtualenv, no dice. It can't find the dependencies.
Is that how this is supposed to work, or have I misconfigured something? Do I need to always be working with the virtualenv active? Seems wrong to me.
Upvotes: 1
Views: 1365
Reputation: 22408
Yes that is the intended behavior. But...
You don't need to "activate" any virtual environment. The important point is to make sure to use the interpreter that is located in the virtual environment.
Say you run python -m venv /path/to/venv
. The virtual environment is created at /path/to/venv
and you will find a Python interpreter executable (or a symbolic link) at /path/to/venv/bin/python
.
You can then call /path/to/venv/bin/python -m pip install flask
. And also /path/to/venv/bin/python /path/to/some_script.py
. These will all take advantage of the virtual environment isolation.
And when you activate the virtual environment, the /path/to/venv/bin
directory is placed at the top of the PATH
environment variable, so that when you type python
, the first python
executable found is automatically /path/to/venv/bin/python
.
Upvotes: 3
Reputation: 220
Actually when you install dependencies in a virtual env then u must activate it before running the project, But u can also install dependencies directly to the system through the terminal (outside of virtual env) and can run the project.
It is preferred to have a virtual environment in order to keep your project dependencies and extra dependencies separate. As for big projects, you can easily collect the necessary dependencies without taking count of which are not used.
Upvotes: 2