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

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.

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 start a flask app from terminal with conda environment

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.

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.

Copy Python App to a new Machine?

I have a Python app running on windows that has imports for the following packages:
requests
json
psycopg2
I copy the entire project (I used Pycharms to write the app and import the packages) to a new machine and expected it would work. The new machine is also windows and I'm trying to run my script from the command line (i.e. no Pycharm on the new machine).
Instead, I get an error saying "ModuleNotFoundError: No module named 'requests'"
If I look at the project, I have the directories:
venv
Lib
site-packages
requests
What am I missing/doing wrong?
You have a couple of options here but first the problem. You are exporting your code base to a new machine without the required Modules installed on that machine and/or within your Python project's environment. After you have python installed on your new machine, you need to be sure to point your PyCharm Project to the proper environment.
File > Default Preferences > Project Interpreter
The window that appears on the right will contain a drop down menu labeled Project Interpreter. If you click on the drop down, it should reveal a list of the available Python environments on your machine.
Based on your description of your site-packages directory I would assume you do not have your interpreter pointed the proper environment on your new machine. With that said, you would be better served creating a new virtual python environment on your machine and installing each relevant dependency within that environment.
Take a look at this post here for your first best option on re-creating your old python environment on your new machine.
EDIT: I apologize for not reading the question more thoroughly before answering the questions. If this is running on a Windows machine you will need to double check the environment path python is using. It is very easy to install python at a different PATH than the command line environment is checking on a Windows box. If for example your PATH is pointing to a different version of Python and PIP is installing packages somewhere else this issue can occur. Double check your System PATH for python and which version the command line is running.
On the new machine you must source venv/bin/activate so your path environment variables are set properly. In particular, which python should say venv/bin/python rather than /usr/bin/python. Also, take care to conda env update or pip install -r requirements.txt so you'll have suitable venv libraries on the new machine.

How to make Python 3 virtual env activated for all the time, even when not using console/putty

I have shared web hosting space with python 2.6.6 pre configured, now I have installed python 3, I have created the virtual env for the same & activated it.
My question is - How can I keep the python3 virtual environment activated all the time, even when am not using the console/putty.
The problem is I have imported couple of libraries to python3 & want to use it, but if my console/putty is closed my header line in .py files has to be pointed back to #!/usr/bin/python >>but this points to python2.6.6.
Whereas I want the python3 should always work. All the users coming to my website, their requests needed to be processed by python3 instead of python2.6.6.
Really searched a lot but could not get this specific information.
Thanks...
You're thinking of this in the wrong way. If they're coming to your website, then they're using your web app. It's only that that needs to run under the virtual environment, which you would configure in its own startup script (eg the wsgi script).

Categories

Resources