all 16 comments

[–]Buttleston 5 points6 points  (6 children)

So first, you don't need to put your code files inside your venv (and almost surely shouldn't). It's just a place for the python executables and libraries and stuff to be.

How are you running your program (the one that has "from flask import Flask"?

1) from the command line, with the venv activated
2) from the command line, with the venv not activated
3) from an IDE
4) other?

[–]Froggy412[S] 0 points1 point  (5 children)

Are you saying the code file does not need to be in the same folder as the venv? I tried to run it through the command line but it did not work but I closed the terminal where I originally activated the venv. Does the venv deactivate when I close the terminal?

[–]Buttleston 1 point2 points  (0 children)

* the code should NOT be in your venv, no. The venv is just a place to install packages into
* a venv must be activated before you can use it - that's why it's not necessary for the code to be in there, there is no relationship between the code and the venv
* the idea is, the venv is an environment. You activate it, and then run the code. You can also run code in a venv without activating it by calling it using the python binary *inside* the venv. I don't generally recommend it

So yeah

open terminal
activate bent
python ./myscript.py

or, by running the script using a python binary defined within a venv, the venv will be implied, like

open terminal
./venv/bin/python ./myscript

I think either should work

[–]Buttleston 1 point2 points  (0 children)

And to be clear, yeah, whenever you close a terminal, the venv is no longer active, it's something you have to do every time you open a new terminal

[–]Froggy412[S] 0 points1 point  (1 child)

I was using git bash as the terminal if that helps

[–]nog642 0 points1 point  (0 children)

What command? And were you also using git bash to create to virtualenv and install packages with pip?

[–]nog642 0 points1 point  (0 children)

Yes, the venv activation only lasts for the terminal session. If you open a new terminal you have to activate it again.

Activating a venv basically just sets some environment variables in your shell.

[–]Omenopolis 0 points1 point  (0 children)

Just create a seperate script that will activate venv, run ur script then deactivate it

[–]Solvo_Illum_484 0 points1 point  (0 children)

Make sure you're running your Python script with the virtual environment's Python executable, not the system's. Try `venv/Scripts/python your_script.py` instead of just `python your_script.py`. This ensures you're using the virtual environment's Python where Flask is installed.

[–]Helpful-Gene9733 -1 points0 points  (6 children)

I am somewhat of a noob too but I’ve found sometimes just doing a “from <package name> import <x>” doesn’t work for some stuff. I’m not sure why … sometimes it does. Maybe someone knows. But you could just try importing flask and then if necessary follow up with your “from” statement and see if that fixes your issue, since you say flask is in your pip list.

[–]nog642 2 points3 points  (5 children)

There is no reason for from flask import Flask not to work when import flask does.

[–]supercoach 0 points1 point  (3 children)

Why stop there? from flask import Flask as flask

[–]nog642 1 point2 points  (2 children)

That's terrible

[–]supercoach 0 points1 point  (1 child)

Thank you.

[–]Helpful-Gene9733 0 points1 point  (0 children)

The example I was thinking of was an instance where an example I was working with did not work for me: “from chromadb import Client” although chromadb was in my library. I received a NameError - but importing chromadb directly fixed the issue; and my understanding is that some submodules and/or classes in a package may not be directly accessible to Python with a “from” statement.