- Web Development with Django Cookbook(Second Edition)
- Aidas Bendoraitis
- 447字
- 2021-07-23 14:31:57
Working with a virtual environment
It is very likely that you will develop multiple Django projects on your computer. Some modules such as Python Imaging Library (or Pillow) and MySQLdb, can be installed once and then shared for all projects. Other modules such as Django, third-party Python libraries, and Django apps, will need to be kept isolated from each other. The virtualenv tool is a utility that separates all the Python projects in their own realms. In this recipe, we will see how to use it.
Getting ready
To manage Python packages, you will need pip
. It is included in your Python installation if you are using Python 2.7.9 or Python 3.4+. If you are using another version of Python, install pip
by executing the installation instructions at http://pip.readthedocs.org/en/stable/installing/. Let's install the shared Python modules Pillow and MySQLdb, and the virtualenv utility, using the following commands:
$ sudo pip install Pillow $ sudo pip install MySQL-python $ sudo pip install virtualenv
How to do it…
Once you have your prerequisites installed, create a directory where all your Django projects will be stored, for example, virtualenvs
under your home directory. Perform the following steps after creating the directory:
- Go to the newly created directory and create a virtual environment that uses the shared system site packages:
$ cd ~/virtualenvs $ mkdir myproject_env $ cd myproject_env $ virtualenv --system-site-packages . New python executable in ./bin/python Installing setuptools………….done. Installing pip……………done.
- To use your newly created virtual environment, you need to execute the activation script in your current shell. This can be done with the following command:
$ source bin/activate
You can also use the following command one for the same (note the space between the dot and bin):
$ . bin/activate
- You will see that the prompt of the command-line tool gets a prefix of the project name, as follows:
(myproject_env)$
- To get out of the virtual environment, type the following command:
$ deactivate
How it works…
When you create a virtual environment, a few specific directories (bin
, build
, include
, and lib
) are created in order to store a copy of the Python installation and some shared Python paths are defined. When the virtual environment is activated, whatever you have installed with pip
or easy_install
will be put in and used by the site packages of the virtual environment, and not the global site packages of your Python installation.
To install Django 1.8 in your virtual environment, type the following command:
(myproject_env)$ pip install Django==1.8
See also
- The Creating a project file structure recipe
- The Deploying on Apache with mod_wsgi recipe in Chapter 11, Testing and Deployment
- HTML5+CSS3+JavaScript從入門到精通:上冊(cè)(微課精編版·第2版)
- Advanced Splunk
- ASP.NET Core 5.0開(kāi)發(fā)入門與實(shí)戰(zhàn)
- R語(yǔ)言數(shù)據(jù)可視化實(shí)戰(zhàn)
- Python編程完全入門教程
- Unity 5.x By Example
- Unity Game Development Scripting
- C語(yǔ)言程序設(shè)計(jì)上機(jī)指導(dǎo)與習(xí)題解答(第2版)
- Scala編程實(shí)戰(zhàn)
- TypeScript圖形渲染實(shí)戰(zhàn):2D架構(gòu)設(shè)計(jì)與實(shí)現(xiàn)
- 人人都能開(kāi)發(fā)RPA機(jī)器人:UiPath從入門到實(shí)戰(zhàn)
- Visual Basic程序設(shè)計(jì)實(shí)驗(yàn)指導(dǎo)及考試指南
- C語(yǔ)言從入門到精通
- Spring MVC Cookbook
- Java編程動(dòng)手學(xué)