Flask runs out of virtual environment - python

I was working in virtual environment but I removed the virtual environment folder in purpose. But I still can run flask restful application as below, while new empty virtual environment(there are no libraries in site packages but even application works like old virtual environment) is active.
flask run
I thought it should have been failed and complaining like:
no module named flask
, because I removed the old proper virtual environment and installed new one then activated it.
And here is the point, when I try the run the main script as below
python3 app.py
It complains as expected:
no module named flask
It seems that when I run with flask run it is not using virtual environment even virtual environment is active. Because of this reason I can't test my application with other python versions and can't test if requirements are installed and run in new other virtual environments. I couldn't understand this behavior of the flask for any help I appreciate.
Thanks in advance.

You may have Flask globally installed.
pip list | grep flask
You can remove that:
pip uninstall flask
or
Create virtualenv with --no-site-packages option:
virtualenv --no-site-packages venv
source venv/bin/activate

Well I figured out what is going on there.
Flask creates a directory named local in home and moved system's interpreter and dependencies there included by flask application that I wrote.
Then it deploys the application from there when run with flask run. So that means my flask app was not running with virtual environment as we guess.
So in that case I recreated my virtual environment and after installing dependencies I run with python3 app.py. Now it seems that it uses the virtual environment dependencies and interpreter.

Related

How do I ensure python project in VSCODE is using virutal envirnoment and not defaulting to global?

Having an issue when using flask and flask-wtf. The issue is that for some reason vscode is defaulting to global rather than the newly created virtual environment. In vscode I will click open folder, create the folder and open it in vscode. Then I'll write my code out and then open terminal in vscode and write: python -m venv my_venv, then on the lower left-hand side I will select it as my virtual environment. I then re-open the terminal, write: pip -V and the pip it is directed to is the folder directory with my_venv in the path name.
Then I pip install flask and flask-wtf, check that they are installed with pip list. Everything looks good until I run and vscode says: ModuleNotFoundError: No module named 'flask-wtf'. But when I go to my global environment and where flask is installed too it runs fine. Something is happening to where the python code is recognizing the original flask and not the one in my virtual environment. How do I ensure that the version of flask is the one in the virtual environment and not others installed on my machine.
Thanks
If you are in your terminal in vscode, it should append your command line with your virtual environment name.
So, usually it would show
PS \path
But if your environment is running, it will show
(venv name) PS \path
If it is now showing, be sure to reactivate your virtual environment from your project directory

Vim ALE cannot import python modules from pipenv

I have setup a virtual environment with Pipenv like so:
$ pipenv install Flask
then I have activated the virtual environment like so:
$ pipenv shell
And then I run my Flask app with:
$ flask run
This works, the Flask server spins up and the page is served successfully.
Then in another terminal I activate the virtual environment again and then launch vim and edit my flask app file.
That's when ALE kicks in and runs Black and returns Cannot find implementation or library stub for module named "flask". Which I understand is because flask cannot be imported from the virtual environment (if I install flask outside the virtual environment then the error disappears).
I have added let g:ale_python_auto_pipenv = 1 as suggested in a similar question (1). But that does not seem to have an effect.
I have also tried to install Black inside the virtual environment as suggested elsewhere, but it does not seem to do anything.
I have read elsewhere (2) that some vim plugins solve that but could not find which to install.
I have also read (2) that the PYTHONPATH environment variable must be set. I have tried to set it to the Pipenv virtual environment to no avail.
What should I do to make vim and ALE aware of the python packages installed in my current virtual environment made with Pipenv?
(as an aside it looks like YouCompleteMe does not complete or go to definitions of those packages as well, but I understand it could be a different solution and thus a different question.)
(1) False import errors running Pylint and MyPy from vim ALE in pipenv. -- :!pylint % works -- :!mypy % works
(2) https://github.com/dense-analysis/ale/issues/1998

templates cannot be found when virtual environment is activated

I am facing a rather peculiar problem. I have built a Django application and only while deploying it, I created a virtual environment(I didn't use virtual environment while building and testing it locally). Now when virtual environment is activated and also when deactivated, the HTML templates cannot be found. Below is the screenshot.
Seeing this statement,
Python Executable: /home/shadowsaint/Programs/Django/zoomtail/venv/bin/python
I went back to terminal and typed which python command. For that it shows
/home/shadowsaint/Programs/Django/zoomtail/venv/bin/python
Now when I deactivate virtual environment also the which python command gives the same thing.
But when I delete the venv file, and run which python it gives,
/usr/bin/python
And now the application runs fine, i.e., the HTML templates are being found.
I have three questions here.
When I activate venv, the python used from venv/bin is fine. But even when I deactivate virtual environment why the same python is being used?
What does python package(both the versions are same) have to do with templates being found?
Is the underlying principle here the same as that in here too - No module named PIL in heroku though it is installed
You have a misconfiguration at your Heroku environment, first check if your requirements was installed, use shell (python manage.py shell) and try import some of them to see if they throw errors... if passes ok then try to access any url from your project... if template breaks maybe the problem is in your Template_DIR at your settings
Obs.: check if your django commands like shell, migrate... works when running heroku run bash with your venv activated
Try follow some of this steps:
https://devcenter.heroku.com/articles/getting-started-with-python#introduction
http://docs.python-guide.org/en/latest/dev/virtualenvs/
Verify in project settings.py that INSTALLED_APPS list actually contains your app name.
INSTALLED_APPS = [
'your app',
'django.contrib...'
.....

Is adding Flask env vars to the virtualenv's activate script OK?

I'm working on my Flask project in a virtualenv. Every time I start a new terminal, I have to reinitialize these Flask environment variables:
export FLASK_APP="server.py"
export FLASK_DEBUG="1"
My goal is to not have to type them in manually.
I tried writing a Python script that set them, but couldn't make it work. I tried writing a shell script that set them, but Flask would raise an error that said my Python path was incorrect.
Finally, I tried adding the the env vars to the bottom of the virtualenv's activate script. It worked! The env vars are set and Flask runs as expected.
$ source venv/bin/activate
$ flask run
Is it OK to modify the activate script like this? This is just for development purposes.
Yes, setting environment variables in the virtualenv's activate script is fine for managing your development environment. It's described in Flask's docs. They're only active when the env is activated in the terminal, and you have to remember to add them if you create a new env, but there's nothing wrong with it.
With Flask 1.0, you can use dotenv files instead. Install python-dotenv:
pip install python-dotenv
Add a .flaskenv file:
FLASK_APP=server
And the flask command will automatically set them when running a command:
flask run
The advantage of this over messing with the venv is that you can commit this file so it applies anywhere you work on the code.
Modifying venv/bin/activate file is working for you because the environment variable is getting defined inside the virtual environment. When you're using python3 -m venv venv the environment variables are not present in the new virtual environment. Instead of modifying the activate file, you can instead make a shell script which:
Runs venv/bin/activate
Defines the environment variables
Runs the server
First, I tried writing a Python script that set them, but after research, I realized it was not possible(?).
You could use os.environ to do the same from within, but a shell script is better.

Python can't find my Flask script under virtualenv

I'm trying to build a simple API server using Flask that will validate and store international phone numbers using this phonenumbers.py. I previously installed Flask and several Flask extensions in a virtualenv, and started building my app. So far so good. But after downloading and installing phone numbers.py using its own installer, I found that python running inside virtualenv could no longer find my app script! Looking at the directory, it's sitting right there, but python does not recognize it any more. The only other wrinkle I can think of is that after doing the install, I used an external text editor (outside the venv) to edit my app script and re-save it.
What have I done wrong?
I compared my environment variables inside and outside virtualenv. They are the same except for the following 3 additions:
VIRTUAL_ENV=/Users/tokrum/callcenter/venv
PATH=/Users/tokrum/callcenter/venv/bin # (was added to the beginning of my $PATH, but the rest of pre-existing PATH is as beforeā€¦.)
PS1=(venv)\h:\W \u\$
My app is called callcenter-v0-1.py. It lives in a directory called /callcenter/, along with the phone numbers-7.0.1 and venv folders at the same level of my directory structure.
Thanks for any light you can shed on this.
Install Flask_script in your virtual env using
$pip install Flask-Script
Make sure that you activated the virtualenv before you installed Flask and your other dependencies -
$ virtualenv env
$ source env/bin/activate
$ pip install flask
Then when you're done working, make sure to deactivate the environment -
$ deactivate
Finally, when you want to start working again, navigate to your project directory and reactivate the environment -
$ source env/bin/activate
At this point, I would just remove the virtualenv and start over.
Hope that helps!

Categories