0

I am trying to run a python script under python 2 virtualenv. How to run it via batch script/ python script?

I have installed both python2 and python 3 and created virtual env too. I tried it invoking via python script but it didn't even enter the virtualenv. Then i tried the below batch script. But It just executed the first line of the code. i.e just activating the virtual environment. but other lines are not getting executed.

I even tried to execute the 1st line of batch script separately in a bat file and then invoke others using perl/python. but none of them worked.

Please do provide a way to execute these commands either using python script or a batch file, which I will need to run it via perl/python

The batch file i used :

C:\venv-2\Scripts\activate
pushd <some path>
python test.py
deactivate
krith
  • 57
  • 10

3 Answers3

0

Just directly use the virtualenv's Python interpreter:

pushd some_path
c:\venv-2\scripts\python test.py
popd
AKX
  • 152,115
  • 15
  • 115
  • 172
  • I tried this, the script runs only in a virtual environment. So using the virtualenv's python interpreter directly dnt work – krith Aug 19 '19 at 11:14
0

Not having a Windows environment to hand, this is a non-answer for *nix which might be adapted to Windows.

Instead of calling the venv bin directly, try activating it within a script. For example:

# my-script.sh

# activate 
source venv/bin/activate

# this should be in the venv
which python3
python3 -c 'print("Hello from python3")'

Then $ bash my-script.sh should print out which python3 it thinks is in use within the script.

See also A Python script that activates the virtualenv and then runs another Python script? for the same idea done better...

0

I got the the batch file executed by giving,

C:\venv-2\Scripts\activate & pushd <some path> & python test.py & deactivate

inside the batch file. And used perl script to call the batch file

system('start test.bat')
krith
  • 57
  • 10