Module not found with virtual environment - python

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.

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.

Why is my Python script not using the virtualenv set for it?

So I don't use virtual env often. In this particular case I need to have a desktop app for a client where they can input a CSV and then I use pandas to modify columns and such and return it to them. For the desktop app I'm using electron, and I already have been able to send and return data between the two processes it's using. To work on this I've been using this tutorial:
https://efficientcoder.net/connect-python-3-electron-nodejs-build-desktop-apps/
So I set up my virtualenv but when I ran the process it said that any module I had installed in the virtualenv was unable to be found. I ran a check and it's using the python from my system not the env from the parent directory. How can I fix this?
calc.py
import sys
print(sys.path)
>>> Outputs: ['C:\\Users\\*Me*\\Projects\\electron-poc\\py', 'C:\\Python310\\python310.zip', 'C:\\Python310\\DLLs', 'C:\\Python310\\lib', 'C:\\Python310', 'C:\\Python310\\lib\\site-packages']

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.

Is it possible to invoke a virtualenv's python interpreter by including a line in the script?

I'm trying to use SQLAlchemy with python. My setup is like so:
mkdir project
cd project
virtualenv project-env
project-env/bin/pip install SQLAlchemy
I activate the environment by navigating to the bin directory and using:
source activate
However I recently just followed some Flask tutorials (Flask also installed in a virtual env) and noticed that this step isn't required to run scripts correctly. https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
In the python file from the tutorial (run.py) there is the line:
#!flask/bin/python
is this setting the path to the interpreter? I've tried:
#!project-env/bin/python
In my script and it doesn't work. Is there a way to do this? Any pointers would be really appreciated! thanks!
That's a terrible shebang line used in the tutorial; it's relative, which means it only works if you happen to be in a working directory that is directly outside the virtual environment.
That is, for that shebang to work, when the virtual env is in ~/foo/bar/project-env, when you run the script, the only way it will work is if you've cded to ~/foo/bar. Even if the script is in ~/foo/bar, it won't work unless you cded there as well.
Use an absolute path, or use a canonical shebang line and actually activate the virtual environment first. I'd recommend the latter (I'm not fully confident that other tweaks made by the activate script aren't important in some cases), but the former will behave as the tutorial you linked expects.

Python: Is there any way how I can know under which virtualenv script is running?

I'm working on project which consists of several subprojects. Each of them uses their own virtualenv. And sometimes I'm not sure that script is running at proper virtualenv. I have a pid of that script in memory.
Is there any way how I know (and be sure that env is correct) under which virtualenv script is running?
I usually decide which virtualenv is running by the absolute path it is. So, from the python script it can be found by next commands:
import os
os.environ.get('VIRTUAL_ENV')
It will get the path from the environment variable VIRTUAL_ENV which is always defined by script for activating virtualenv.

Categories

Resources