Unable to import Flask without virtual enviroment - python

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.

Related

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.

Python venv programmatically

We are trying to add venv functionality to our python project ie when one runs project an venv is created in project path and project runs on that venv.
For this to work we used vurtualenv's create environment method and activatethis.py the method creates venv ,
However when project runs it still uses host machine packages. Instead of using venv ones
Am I doing anything wrong..is it possible to do this??
PS: I have tried both site-packages true and false in create environment method .. however to no help
Adding more info:
Commands used are
venv_dir = os.path.join("path to project")
virtualenv.create_environment(venv_dir)
execfile(os.path.join(venv_dir, "bin", "activate_this.py"))
Also updated path environment variable to include bin of venv
After this I expect my code (after this) to run in virtual environment.
I am not using any ide and expect the code to create venv at runtime
Using python 2.7
Is there any other way to run project in virtual environment at run time without any ide in Ubuntu server environment.
You haven't provided enough information for us to come up with a solution to your exact problem, although I will do my best to help you out.
You should not be creating a venv every time you run the project, which it sounds like you are doing. This is essentially creating a new interpreter every single time you run your project. If you're using a decent IDE, such as PyCharm, you should be able to set this up properly through the project settings.
There are plenty of tutorials out there regarding setting up venv and virtualenv on your computer, and given that we don't know much about your system, your best bet is doing some further research.
Essentially, you need to
Create a venv for the project (and a new one for each project you make from here on out, unless you want to use the machine's interpreter)
Set the project interpreter to the venv you just created. Place any external libraries within ...\venv\Lib\site-packages
Hope this helps.

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.

Associating a python project with a virtual environment

been searching for this with no success, i don't know if i am missing something but i have a virtualenv already but how do i create a project to associate the virtualenv with, thanks
P.S. Am on windows
I could be wrong here, but I do not believe that a virtualenv is something that is by its very nature something that you associate with a project. When you use a virtualenv, you're basically saying, "I'm taking this Python interpreter, installing what I want on it, and setting it aside from the Python interpreter that the entire computer uses by default." Virtualenv does not have a concept of a Python "project"; it is just a custom version of a Python interpreter that you run code through. There are tools in IDEs like PyCharm that enable you to associate a project with a virtualenv, but those are another layer on top of the base software.
In order to use a virtualenv with a project, you will need to "activate" it every time you wish to use it. The documentation for activating a virtualenv on Windows is found here.
EDIT:
Saw that you had virtualenvwrapper tagged in your post, so I did a bit of hunting on that. It would appear that there is the mkproject command, which creates a project folder and then associates it with a virtualenv interpreter. Documentation on it can be found here.
Requirements:
Virtual Env
Pycharm
Go to Virtual env and type which python
Add remote project interpreter (File > Default Settings > Project Interpreter (cog) add remote)
You'll need to set up your file system so that PyCharm can also open the project.
NOTE:
do not turn off your virtual environment without saving your run configurations that will cause pycharm to see your run configurations as corrupt
There's a button on the top right that reads share enable this and your run configs will be saved to a .idea file and you'll have a lot less issues
If you already have your virtualenv installed you just need to start using it.
Create your projects virtual environment using virtualenv env_name on cmd. To associate a specific version of python with your environment use: virtualenv env_name -p pythonx.x;
Activate your environment by navigating into its Scripts folder and executing activate.
Your terminal now is using your virtual environment, that means every python package you install and the python version you run will be the ones you configured inside your env.
I like to create environments with the names similar to my projects, I always use one environment to each project, that helps keeping track of which packages my specific projects need to run.
If you haven't read much about venvs yet, try googling about requirements.txt along with pip freeze command those are pretty useful to keep track of your project's packages.
I like Pipenv: Python Dev Workflow for Humans to manage environments:
Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world. Windows is a first-class citizen, in our world.
It automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile as you install/uninstall packages. It also generates the ever-important Pipfile.lock, which is used to produce deterministic builds.
Pipenv is primarily meant to provide users and developers of applications with an easy method to setup a working environment.

How to disable flask global installation

I have created a python project using Flask.Let's call it projectA. I have run the command . flask/bin/activate to make it global. I have created another project called projectB. I ran the same command to make this Flask installation global. Next I try to install python-mysql module in the projectB. However, I noticed that it gets installed in the projectA.
How to fix this issue?
I assumed that If I can deactivate projectA global installation, this issue may be fixed. However, I didn't find a suitable command in the Flask documentation.Even though, I deleted the projectA, it still try to install the mysql module in the projectA.
I don't believe that I got it right: what are your flask folder contents?
I ask it because sometimes it is the virtualenv – a lot of Flask tutorials suggest installing a virtualenv under the flask folder.
If that is the case, it doesn't make the project global. On the contrary: it makes your commands use the local version of Python (the one installed inside the flask/bin folder), and not the global Python installed wihin your operational system.
So, your problem might not be with Flask itself, but with the lack of understanding of virtualenv.
When you run . flask/bin/activate inside project A, whatever you do in terms of Python (pip and easy_install included) will reflect only in the Python installation under project A's flask folder. Until you run deactivate. Does that make sense?
So, maybe the command you need is deactivate so you can jump from one virtualenv to another.
And, as a final advice, take some time to study virtualenv and, from there, go to virtualenvwrapper.

Categories

Resources