How to make sure that the project is under a virtualenvironment?

Hello guys,

So I just set up a virtual environment using Virtualenv to create my microblog project. As I reading the tutorials on how to make sure that your virtual enviroment is activated, it said once you see the (env)prefix from the cmd, like the picture below:

that means you are sucessfully activated your virtual envionment.
I did get the (env)prefix as I activated my virtual enviroment. However, whenever I close the cmd and open it again, the (env) is no longer there:

Once I activate it again, it reappeared:

I am bit confused: Do I actually activate or not? Should the (env)prefix appear every time when I open cmd?

Thank you everyone!

By Timjinlun via Discussions on Python.org at 28Jun2022 05:38:

So I just set up a virtual environment using Virtualenv to create my
microblog project. As I reading the tutorials on how to make sure that
your virtual enviroment is activated, it said once you see the
(env)prefix from the cmd, like the picture below:
[ CMD prompt with the “activation” customisations … ]
that means you are sucessfully activated your virtual envionment.

I did get the (env)prefix as I activated my virtual enviroment. However, whenever I close the cmd and open it again, the (env) is no longer there:


Once I activate it again, it reappeared:

I am bit confused: Do I actually activate or not? Should the (env)prefix appear every time when I open cmd?

No, activate is a purely local thing to set your current command
environment to use a virtual environment by default. So a new command
window will not have the environment “activated” automatically. It might
be possible to do this on Windows, but I do not know how (I’m a UNIX
person).

Cheers,
Cameron Simpson cs@cskk.id.au

To use a virtual environment, run files directly from its “Scripts” directory, including “python.exe”, “pip.exe”, and other installed scripts. The virtual environment doesn’t have to be activated for this to work.

If the py launcher is installed and set as the default application for .py[w] files, then Python scripts on your system support Unix-like shebang lines. This is the default installation configuration when Python 3.5+ is installed using a standard installer from python.org. In this case, you can ensure that a script runs in the virtual environment by making the first line of the script a shebang that contains the path of “python.exe” in the environment. For example:

#!"path\to\env\Scripts\python.exe"

Change “path\to\env” to the actual location of the virtual environment.

Activating an environment is a convenience for developing modules and scripts that use the environment. It sets up various environment variables such as “PATH” and “PROMPT” to customize the current shell to prefer the virtual environment.

You can create a shortcut on your desktop that opens a new console session with the virtual environment activated. When prompted for the target location, enter the following:

%SystemRoot%\System32\cmd.exe /k "path\to\env\Scripts\activate.bat"

Change “path\to\env” to the actual location of the virtual environment. Name the shortcut whatever you like, and then modify its properties to set the initial working directory to be the project directory.

3 Likes