-1

enter image description hereI am just starting to learn django and I am facing the set-up phase. In particular I'd like to ask how to recognize if the virtual environment is activated or not. I know that I can use the command pip freeze but in all the tutorial that I am following, when the venv is activated, I can see the venv name in brackets in the terminal command line. I can correctly activate the venv with the source command and check via the pip freeze command but I have no indication in the command line. I am on a Mac/OS(M1) and using python3

thank you

ranran212
  • 45
  • 9
  • Please show us what you're doing exactly and what you see… – deceze Aug 19 '22 at 10:03
  • Does this answer your question? [How to make sure that my django project is using virtual environment that i created for it?](https://stackoverflow.com/questions/61005013/how-to-make-sure-that-my-django-project-is-using-virtual-environment-that-i-crea) – enes islam Aug 19 '22 at 11:07
  • as you can see, i added a screenshot. My question is clear, I am asking why I do not see (menv) in the command line, I didn't ask how to activate the venv as I clearly stated that the venv is activated and works.! – ranran212 Aug 19 '22 at 12:20

3 Answers3

1

To have visual information about the virtualenv on the command line you need to change the shell config to show it. It's not related to python or django itself.

It will depend on the shell that you are using, but assuming the default shell on mac you can check this question virtualenv name not show in zsh prompt

Andreu Gallofré
  • 1,245
  • 9
  • 14
1

From venv docs.

When a virtual environment is active, the VIRTUAL_ENV environment variable is set to the path of the virtual environment. This can be used to check if one is running inside a virtual environment.

So you should be able to test it with:

import os

os.getenv('VIRTUAL_ENV') is not None
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
igavran
  • 181
  • 6
0

Yes, when the virtual environment is activated, it shows in the terminal as a prefix like

(env) yourname~Desktop/workspace-folder> 

And that is enough to know it is activated, and you are using it.

Since you are using Python 3, you can create your virtual environment as follows in the same directory of your project, open terminal or iTerm and run this comman

python3 -m venv env

After few seconds, a folder env will be created that is your virtual environment. Note you can name your virtual environment as you wish, maybe like python3 -m venv djang-app

After you have created the virtual environment, you can now activate it like

source env/bin/activate

Again, if you created your virtual environment using lets say django-app

python3 -m venv django-app

You can activate it like this

source django-app/bin/activate

and you will see your terminal is prefixed as follows

(django-app) yourname~Desktop/workspace-folder> 

To deactivate the virtual environment, you simply run this command deactivate in the terminal with active virtual environment.

You can learn more about python3 venv from here.

sikaili99
  • 532
  • 6
  • 14