How to run a python script with Python Tools for Visual Studio in a virtualenv?
Asked Answered
R

4

6

I don't know how to run the activate.bat in a Python Tools for Visual Studio Project. I have a directory environment in my project with my virtualenv. But, I don't know how I can run ./env/Scripts/activate.bat before the project run my main python script.

Rixdollar answered 6/8, 2012 at 8:6 Comment(0)
R
4

I found that if :

  • main.py is set as Startup File,
  • in the Properties of the project -> Debug tab -> Interpreter Path field, I put the path C:...\env\Scripts\python.exe (i.e. the python executable of the virtualenv)

It works !

Rixdollar answered 6/8, 2012 at 10:0 Comment(4)
Your question was how I can run ./env/Scripts/activate.bat, you've unaccepted my answer, posted your own answer that isn't relevant to the initial question, and then accepted it. Bad form.Fumed
I prefer this answer from balmat : not tampering the source, just the environment.Overarch
Nice! However, it's not necessary to have the startup script called main.py. You only need to point the interpreter into the virtualenv, as you said.Diagnostic
Hi @Balmat, How to set main.py as Startup File?Fushih
F
4

I usually point Visual Studio to a custom startup.py script, any other batch files I can then run using:

# startup.py
import os
import main.py # Whatever your main script is
os.system('activate.bat') # Call your batch files.
main.run() # Call whatever you need to from your main script.

In Visual Studio

  • Right click on project
  • Properties
  • General
  • Under Startup File, put startup.py (whatever)
  • Make sure your working directory is correct
Fumed answered 6/8, 2012 at 8:29 Comment(1)
I'd say, @balmat own answer is most appropriate (and straightforward) for the his questionOverarch
R
4

I found that if :

  • main.py is set as Startup File,
  • in the Properties of the project -> Debug tab -> Interpreter Path field, I put the path C:...\env\Scripts\python.exe (i.e. the python executable of the virtualenv)

It works !

Rixdollar answered 6/8, 2012 at 10:0 Comment(4)
Your question was how I can run ./env/Scripts/activate.bat, you've unaccepted my answer, posted your own answer that isn't relevant to the initial question, and then accepted it. Bad form.Fumed
I prefer this answer from balmat : not tampering the source, just the environment.Overarch
Nice! However, it's not necessary to have the startup script called main.py. You only need to point the interpreter into the virtualenv, as you said.Diagnostic
Hi @Balmat, How to set main.py as Startup File?Fushih
P
4

Python Tools for Visual Studio(PTVS) 2.0 is out now, in it you can add a virtualenv.

  1. Open the Solution Explorer: View > Solution Explorer

  2. Right click on 'Python Environments' and choose 'Add Virtual Environment'

Here is a video showing how to do it.

Parity answered 6/3, 2016 at 1:12 Comment(0)
U
1

Full support for Virtual Env is coming in PTVS 2.0 Beta/RTM. See http://pytools.codeplex.com for news/updates. Early support is in PTVS 2.0 Alpha, available now.

Upgrowth answered 9/5, 2013 at 23:40 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.