- Django 2 by Example
- Antonio Melé
- 243字
- 2021-06-25 21:18:58
Creating an isolated Python environment
It is recommended that you use virtualenv to create isolated Python environments, so that you can use different package versions for different projects, which is far more practical than installing Python packages system-wide. Another advantage of using virtualenv is that you won't need any administration privileges to install Python packages. Run the following command in your shell to install virtualenv:
pip install virtualenv
After you install virtualenv, create an isolated environment with the following command:
virtualenv my_env
This will create a my_env/ directory, including your Python environment. Any Python libraries you install while your virtual environment is active will go into the my_env/lib/python3.6/site-packages directory.
If your system comes with Python 2.X and you have installed Python 3.X, you have to tell virtualenv to use the latter.
You can locate the path where Python 3 is installed and use it to create the virtual environment with the following commands:
zenx$ which python3
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3
zenx$ virtualenv my_env -p /Library/Frameworks/Python.framework/Versions/3.6/bin/python3
Run the following command to activate your virtual environment:
source my_env/bin/activate
The shell prompt will include the name of the active virtual environment enclosed in parentheses, as follows:
(my_env)laptop:~ zenx$
You can deactivate your environment at any time with the deactivate command.
You can find more information about virtualenv at https://virtualenv.pypa.io/en/latest/.
On top of virtualenv, you can use virtualenvwrapper. This tool provides wrappers that make it easier to create and manage your virtual environments. You can download it from https://virtualenvwrapper.readthedocs.io/en/latest/.