4

I want to create a bootstrap script for setting up a local environment and installing all requirments in it. I have been trying with virtualenv.create_bootstrap_script as described in their docs.

import virtualenv
s = virtualenv.create_bootstrap_script('''
import subprocess
def after_install(options, home_dir):
  subprocess.call(['pip', 'install', 'django'])
''')
open('bootstrap.py','w').write(s)

When running the resulting bootstrap.py, it sets up the virtual environment correctly, but it then attempts to install Django globally.

How can I write a bootstrap script that installs Django only in this local virtual environment. It has to work on both Windows and Linux.

4 Answers 4

2

You could force pip to install into your virtualenv by:

subprocess.call(['pip', 'install', '-E', home_dir, 'django'])

Furthermore, it is a nice and useful convention to store your dependencies in requirements.txt file, for django 1.3 that'd be:

django==1.3

and then in your after_install:

subprocess.call(['pip', 'install', '-E', home_dir, '-r', path_to_req_txt])
Sign up to request clarification or add additional context in comments.

Comments

0

You need to pass it the fully qualified path to the pip script that is in your virtualenv.

subprocess.call([join(home_dir, 'bin', 'pip'),'install','django'])

2 Comments

Thanks. That will not work under Windows, since it would be './Scripts/pip.exe'. Do I really have to make a special case for this? Or is there a better way?
I don't think you have to do this, pip's activate augments PATH variable afaik
0

A solution that works on both Windows and Linux. It uses the pip, just installed by the bootstrap script.

import virtualenv
s = '''
import subprocess, os
def after_install(options, home_dir):
  if os.name == 'posix':
    subprocess.call([os.path.join(home_dir, 'bin', 'pip'), 'install', '-r', 'requirements.txt'])
  else:
    subprocess.call([os.path.join(home_dir, 'Scripts', 'pip.exe'), 'install', '-r', 'requirements.txt'])
'''
script = virtualenv.create_bootstrap_script(s, python_version='2.7')
f = open('bootstrap.py','w')
f.write(script)
f.close()

Just put your requirements in requirements.txt, one line for every package:

django
django-registration==1.4.3

See: Pip - Requiremments Files

Comments

-1

What worked for me is to access pip from the newly created environment.

pip = os.path.join(home_dir, 'bin', 'pip')

And after that, I try to install django as you previously did.

subprocess.call([pip, 'install', 'django'])

Remember the os import:

import os, subprocess

Hope it works for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.