Virtual environments

Python virtual environments, or virtualenv for short, are isolated Python workspaces. These 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/ subdirectory and a lib/python3.5 subdirectory. Don't forget -p python3, or you are likely to get a Python 2.7 virtual environment that won't be able to run Odoo 12.0.

In bin/, you will find several scripts:

  • activate: This script is not executed and is sourced using the shell's built-in source command. This will activate the environment by adjusting the PATH environment variable to include the bin/ directory of virtualenv. It also installs a shell function called deactivate, which you can run in order to exit 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 virtualenv only.
  • python3: This is a wrapper around your system's Python interpreter, which uses the packages that have been installed in 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.) This means you can use . ~/odoo-12.0/bin/activate instead of source ~/odoo-12.0/bin/activate. The shortcut form is perfectly fine, but we will stick to the source in this book for the purpose of readability.

There are two main ways of using a virtualenv. You may activate it, as shown in this 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 script. This is mainly a matter of taste, so you should experiment and find out which style suits you better.

You may have executable Python scripts within the first line. This should look as follows:

#! /usr/bin/env python3

These will be easier to use with an activated virtualenv script.

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
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset