Replicating/reproducing the Django development env

2020-04-12 02:45发布

问题:

I am working with my friends on a Django project. The project has dependencies on some python modules. I have django and those additional dependencies installed inside a virtualenv. The code of the django project is in a repository accessible to all the friends who can checkout/clone and then contribute code to it. But is there a way to replicate the setup that I have in my development environment in my friends' computers, i.e., something that will install all the additional dependencies and get the environment ready for deployment?

I have heard about zc.buildout. Just had a look at it without going too deep. It does appear complex. Are there other ways to achieve this? The development environments used by my friends vary from GNU/Linux to MS Windows.

回答1:

buildout.cfg:

[buildout]
parts = python

[python]
recipe = zc.recipe.egg
eggs =
    your
    egg
    dependencies
    here
interpreter = python

Get bootstrap.py. Then:

$ python bootstrap.py
$ bin/buildout
$ bin/python ...


回答2:

virtualenv has a neat feature in which it creates a copy of itself with a couple more hooks. In your case the important hook is after_install, which will be executed just after the virtualenv is installed.

Just create a script with the following content:

import os, virtualenv

extra_text = """
import os, subprocess
def after_install(options, home_dir):
    subprocess.call([
        os.path.join(home_dir, 'bin', 'pip'),
        'install',
        '-r', 
        'relative_path_from_env_home_to_requirements_file',
    ])

def adjust_options(options, args):
    if not args: args.append('.')
"""

output = virtualenv.create_bootstrap_script(extra_text)
open('bootstrap.py', 'w').write(output)

And execute it. It will create a bootstrap.py file that your fellow must execute to bootstrap both the virtualenv and the required packages:

./bootstrap.py --no-site-packages

The virtualenv is created at the root of the project, so be sure to svn:ignore or .gitignore the created dirs before committing.

The only drawback of this is that AFAIK it's not integrated with virtualenvwrapper. But anyway the raison d'être of this is to have the environment in the project, and the one of virtualenvwrapper is to have the environments in your homedir.