- Mastering OpenCV 4 with Python
- Alberto Fernández Villán
- 531字
- 2021-07-02 12:07:10
Installing Python, OpenCV, and other packages with virtualenv
virtualenv (https://pypi.org/project/virtualenv/) is a very popular tool that creates isolated Python environments for Python libraries. virtualenv allows multiple Python projects that have different (and sometimes conflicting) requirements. In a technical way, virtualenv works by installing some files under a directory (for example, env/).
Additionally, virtualenv modifies the PATH environment variable to prefix it with a custom bin directory (for example, env/bin/). Additionally, an exact copy of the Python or Python3 binary is placed in this directory. Once this virtual environment is activated, you can install packages in the virtual environment using pip. virtualenv is also recommended by the PyPA (https://packaging.python.org/guides/tool-recommendations/). Therefore, we will see how to install OpenCV or any other packages using virtual environments.
Usually, pip and virtualenv are the only two packages you need to install globally. This is because, once you have installed both packages, you can do all your work inside a virtual environment. In fact, virtualenv is really all you need, because this package provides a copy of pip, which gets copied into every new environment you create.
Now, we will see how to install, activate, use, and deactivate virtual environments. Specific commands are given now for both Linux and Windows operating systems. We are not going to add a specific section for each of the operating systems, because the process is very similar in each one. Let's start installing virtualenv:
$ pip install virtualenv
Inside this directory (env), some files and folders are created with all you need to run your python applications. For example, the new python executable will be located at /env/scripts/python.exe. The next step is to create a new virtual environment. First, change the directory into the root of the project directory. The second step is to use the virtualenv command-line tool to create the environment:
$ virtualenv env
Here, env is the name of the directory you want to create your virtual environment inside. It is a common convention to call the directory you want to create your virtual environment inside env, and to put it inside your project directory. This way, if you keep your code at ~/code/myproject/, the environment will be at ~/code/myproject/env/.
The next step is to activate the env environment that you have just created using the command-line tool to execute the activate script, which is in the following location:
- ~/code/myprojectname/env/bin/activate (Linux)
- ~/code/myprojectname/env/Scripts/activate (Windows)
For example, under Windows, you should type the following:
$ ~/code/myprojectname/env/Scripts/activate
(env) $
Now you can install the required packages only for this activated environment. For example, if you want to install Django, which is a free and open source web framework, written in Python, you should type this:
(env)$ pip install Django
You can also deactivate the environment by executing the following:
$ deactivate
$
You should see that you have returned to your normal prompt, indicating that you are no longer in any virtualenv. Finally, if you want to delete your environment, just type the following:
$ rmvirtualenv test
- Xcode 7 Essentials(Second Edition)
- 羅克韋爾ControlLogix系統應用技術
- UI智能化與前端智能化:工程技術、實現方法與編程思想
- CKA/CKAD應試教程:從Docker到Kubernetes完全攻略
- Mastering RStudio:Develop,Communicate,and Collaborate with R
- Mastering Ext JS
- 琢石成器:Windows環境下32位匯編語言程序設計
- iPhone應用開發從入門到精通
- Visual Basic程序設計習題與上機實踐
- R語言數據可視化:科技圖表繪制
- 并行編程方法與優化實踐
- HTML5+CSS3+JavaScript 從入門到項目實踐(超值版)
- Windows Phone 8 Game Development
- HTML5與CSS3權威指南
- Learning Alfresco Web Scripts