Odoo 11 Development Cookbook(Second Edition)
上QQ阅读APP看书,第一时间看更新

Virtual environments

Python virtual environments, or virtualenv for short, are isolated Python workspaces. They are very useful to Python developers because they allow different workspaces with different versions of various Python libraries to be installed, possibly on different Python interpreter versions.

You can create as many environments as you wish using the virtualenv -p python3 path/to/newenv command. This will create a newenv directory in the specified location, containing a bin/ sub-directory and a lib/python3.5 subdirectory. Don't forget -p python3, or you are likely to get a python 2.7 virtual environment that will not be able to run Odoo 11.0.

In bin/you will find several scripts:

  • activate: The script is not executed; it is sourced using the shell built-in source command. This will activate the environment by adjusting the PATH environment variable to include the bin/ directory of the virtualenv. It also installs a shell function called deactivate, which you can run to exit the virtualenv, and changes the shell prompt to let you know which virtualenv is currently activated.
  • pip3: This is a special version of the pip3 command that acts inside the virtualenv only.
  • python3: This is a wrapper around your system Python interpreter that uses the packages installed in the virtualenv.

The shell built-in source command is also available as . (a single dot, followed by a space, and the path to the file to source). The shortcut form is perfectly fine, but we will stick to source in this book for readability.

There are two main ways of using a virtualenv. You may activate it as we show in the recipe (and call deactivate when you're done), or you may use the scripts in the bin/ directory of the environment explicitly by calling them with their full path, in which case you don't need to activate the virtualenv. This is mainly a matter of taste, so you should experiment and find out which style suits you better for which case.

You may have executable Python scripts with the first line looking like the following:

#! /usr/bin/env python3
 

These will be easier to use with an activated virtualenv.

This is the case with the odoo-bin script, which you can call in the following way:

$ ./odoo-bin -d odoo-test --addons-path=addons --db-filter=odoo-test