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.
Related
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.
I've put myself in a big ol' pickle. This morning, I unsynced some heavy folders from Box in order to save on power consumption and memory. What I didn't realize is that for some reason years ago I installed python within this synced drive that carries all my research. So a lot of dependencies broke after I unsynced a folder called .local. Since then I've re-synced the folder just to have it working for the day but it has 20,000 files in it and it's taking an eternity to patch things up. Little by little, I'm able to load more libraries, but it's just a bandaid and ultimately what I want to do is move all my python stuff from that synced drive to a local directory.
I'm using WSL. I've tried "uninstalling" and reinstalling python3 using the Windows installer, but whenever I try to run a .py program from the terminal, I get the same errors as before, such as:
cannot import name '_np_version_under1p14' from 'pandas.compat.numpy'
and a little while later (after some files synced)
AttributeError: module 'pandas' has no attribute 'read_csv'
So it appears to still reference the same place that is currently missing some files due to the sync problem. That means it's still referencing the directory within the synced drive. How can I tell the terminal to use the packages installed elsewhere? I see a directory in /mnt/usr/local/lib/python3.6/ that contains the site-packages I think I need. How can I use the terminal command python3 to look for files in this other directory by default?
It seems like you're using the global python interpreter to run your scripts which is usually not the best thing to do.
You will run into dependency issues or things breaking like you described.
Much better way is to create a virtual environment, install all your dependencies like pandas, and run your scripts from there.
Create new virtual environment:
python -m venv .venv
Activate it:
source .venv/bin/activate
Install dependencies:
pip install pandas
And then run your scripts.
If you have a lot dependencies you can use tools like python poetry to manage them. Also you'll get virtual environments helpers and locking for free.
https://python-poetry.org/
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.
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.
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.