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.
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])
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'])
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.