4

I created a virtual environment for a Python program using PyCharm. I can run the program just fine straight from PyCharm terminal but when I try to run it from windows command prompt I get an error because some modules in the program are only installed in the virtual environment.

So my question is: how do I run a Python program that is in a virtual environment from the command prompt?

Tulio Franzini
  • 109
  • 1
  • 2
  • 10

2 Answers2

7
$ cd your_project
$ ./activate.bat
$ python helloworld.py

Explaination: virtualenv should create a .bat file in your project root directory. To run it, activate the virtual env first then run the python command.

Ewran
  • 328
  • 4
  • 15
Travis Tay
  • 350
  • 2
  • 10
  • 2
    venv activation is a convenience for interactive work. It sets the venv's "bin" (Unix) or "Scripts" (Windows) directory at the start of `PATH` and also sets the `VIRTUAL_ENV` environment variable (which py.exe in Windows looks for in some cases). But if you explicitly run the qualified path of "python[.exe]" in the venv, you do not need to activate it. In particular, in Windows if the py launcher is installed an associated with .py files, you can run a script directly that uses the qualified path of the venv python.exe in its shebang line (e.g. `#!"C:\venv\someenv\Scripts\python.exe"`). – Eryk Sun Jun 09 '20 at 15:18
  • @ErykSun This is great, thanks! The shebang can even be relative: `#!".venv\Scripts\python.exe"`. – Daniel Saner Nov 23 '22 at 15:45
1

I think you have to look after switching virtual environment on windows

may be this will help [https://towardsdatascience.com/manage-your-python-virtual-environment-with-conda-a0d2934d5195][1]

yhn
  • 141
  • 1
  • 5