How to start a flask app from terminal with conda environment - python

My Flask app environment is using anaconda
When I use IDE like PyCharm, it provides a terminal which already in the specific conda environment, like this:
It could easily let me to start the app via command flask run
Then my question is, how could I start the app in an original terminal?
I tried to use flask run in the original terminal but the dependency module are not found (All dependencies are placed in the conda environment actually)

You can use conda run for this. E.g.,
conda run -n flask_env_3 flask run
You may need to add the flag(s) --live-stream or --no-capture-output (or both), for interactivity.

Related

Unable to import Flask without virtual enviroment

So, I have my folder, inside it when I create my venv I can use Flask just fine inside my folder.
However, when I start a new project, I do not have a venv yet. I create my server.py file, and I try to "from flask import Flask" but its all grayed out saying:
"Import "flask" could not be resolvedPylancereportMissingImports"
What is the reason for this, and why do I always need to create a venv for flask to work in my folder? I think it is supposed to work without a venv.
Not working
Working with venv
Do you have flask installed globally?
When you're using a virtual environment, your IDE knows which Python binary to use and the available packages installed. It is therefore aware of flask and can provide language assistance + all of the other relevant features.
When you aren't in a virtual environment, your IDE is defaulting to some Python version on your computer that doesn't have flask as an installed package.
why do I always need to create a venv for flask to work in my folder? I think it is supposed to work without a venv.
As someone who has coded in Python for over 5 years, I highly encourage you to always use a virtual environment. Venvs allow us to avoid installing Python packages globally which could break system tools or other projects.

How to tell Flask (.flaskenv) which python to use

I have a flask project and specified a .flaskenv file like this:
FLASK_APP=my_program.py
FLASK_DEBUG=1
I'm running this on a MacOS system with Python 3.8.x.
I'm calling flask run to start the program. This works fine, but I want the program to run in a conda environment, however, no matter in which conda environment I'm using, the program will always run in the local python environment.
Just using python my_program.py will work and use the desired conda environment that is active in the shell.
But, if possible, I would like to specify the python environment that is used when calling flask run.
Is there some way to specify the desired python environment for example in .flaskenv?
Edit:
Installing flask via conda in the respective environment solved the problem for me. For further details see answer by #VPfB and comments.
As discussed in the comments, the flask Python script is not able to select the environment it will run inside, because it is a part of some environemnt.
The script contains startup code which imports the entry point function from the flask library (package) and invokes it. The script is part of the package and the package is installed in some Python environment (or system-wide).
In order to be able to run a flask app in multiple environments, it must be installed in each of them.

Module not found with virtual environment

I can run my app from the console that there is in pyCharm but If I try to run my app from a shell my app doesn't find "pymysql" module.
The module is installed in my project in a virtual environment. You can see in the next image how is installed this module.
And If try to run my app from the shell I've got this error:
I'm using python3.
What am I doing wrong? Is there any easy way to access to the module?
There are several ways:
activate virtual env: source venv/bin/activate.
directly use specific python: venv/bin/python main.py
Surely you can temporarily add venv/bin to your PATH, that's almost the same as the first option: export PATH=full/path/to/bin:$PATH
Generally I recommend the first option. But sometimes you may want to use the second one. For example, you want to use this python in a crontab script.

Cloud9 Python: getting Flask module not found on Run action

I started an empty Ubuntu (Ubuntu 14.04.3 LTS) workspace on cloud9. Python 2.7.6 was there by default. I installed python-pip and python-virtualenv (using apt-get).
Having created the smallest possible Flask application I've faced a problem: I get an import error if I press ide's Run button:
Update
The same problem here: https://community.c9.io/t/not-able-to-run-python-file-which-has-a-import-statement-for-flask-installed-via-virtualenv-py/6151
If I run the application from terminal by issuing python app.py then everything's fine:
Virtual environment is activated.
pip list shows
...
Flask (0.12) - it's there.
...
which python shows
/home/ubuntu/workspace/env/bin/python
What's wrong with my setup?
If running from the terminal after activating the environment works fine maybe you could include the command to activate the virtual environment from within the script, for example:
import os
os.system("source env/bin/activate")
import flask
you may need to use an absolute path for the environment but that should be easy enough to find

Installing Anaconda on Amazon Elastic Beanstalk

I've added deploy commands to my Elastic Beanstalk deployment which download the Anaconda installer, and install it into /anaconda. Everything goes well, but I cannot seem to correctly modify the PATH of my instance to include /anaconda/bin as suggested by the Anaconda installation page. If I SSH into an instance and manually add it, everything works fine. But this is obviously not the correct approach, as machines will be added automatically by EB.
So my question is: how can I use Anaconda in my script?
A couple more details:
I've tried adding /anaconda/bin to the system PATH all ways I can think of. Pre/post deploy scripts, custom environment variables, etc. It seems that no matter what I do, the modifications don't persist to when the application is run.
I've tried to include Anaconda via adding it to sys.path: sys.path.append('/anaconda/bin')
to no avail. Using the following: sys.path.append('/anaconda/lib/python2.7/site-packages') allows me to import some packages but fails on import pandas. Strangely enough, if I SSH into the instance and run the application with their python (/opt/python/run/venv/bin/python2.7) it runs fine. Am I going crazy? Why does it fail on a specific import statement when run via EB?
Found the answer: import pandas was failing because matplotlib was failing to initialize, because it was trying to get the current user's home directory. Since the application is run via WSGI, the HOME variable is set to /home/wsgi but this directory doesn't exist. So, creating this directory via deployment command fixed this issue.
My overall setup to use Anaconda on Elastic Beanstalk is as follows:
.ebextensions/options.config contains:
commands:
00_download_conda:
command: 'wget http://repo.continuum.io/archive/Anaconda-2.0.1-Linux-x86_64.sh'
test: test ! -d /anaconda
01_install_conda:
command: 'bash Anaconda-2.0.1-Linux-x86_64.sh -b -f -p /anaconda'
test: test ! -d /anaconda
02_create_home:
command: 'mkdir -p /home/wsgi'
00_download_conda simply downloads Anaconda. See here for latest Anaconda version download link. The test commands are EB's way of letting you only execute the command if the test fails...Just prevents double downloading when in development.
01_install_conda installs Anaconda with options -b -f -p /anaconda which allows it to be installed in the specified directory, without user input, and skips installation if it has already been installed.
02_create_home creates the missing directory.
And finally - to use Anaconda inside your python application: sys.path.append('/anaconda/lib/python2.7/site-packages')
Cheers!

Categories

Resources