virtualenvwrapper

VirtualEnv 是什么

VirtualEnv用于在一台机器上创建多个独立的python运行环境,VirtualEnvWrapper为前者提供了一些便利的命令行上的封装。
为什么要用

- 隔离项目之间的第三方包依赖,如A项目依赖django1.2.5,B项目依赖django1.3。
- 为部署应用提供方便,把开发环境的虚拟环境打包到生产环境即可,不需要在服务器上再折腾一翻。

怎么用
安装

- pip install virtualenvwrapper
- 把下面这句加到~/.bash_profile里面,如不嫌麻烦,也可以每次都手动执行。
source /usr/local/bin/virtualenvwrapper.sh

常用命令

创新的虚拟环境
- mkvirtualenv [env1]
该命令会帮我们创建一个新环境,默认情况下,环境的目录是.virtualenv/en1,创建过程中它会自动帮我们安装pip,以后我们要安装新依赖时可直接使用pip命令。
创建完之后,自动切换到该环境下工作,可看到提示符变为:
(env1)$
在这个环境下安装的依赖不会影响到其他的环境
- lssitepackages 显示该环境中所安装的包

切换环境
- workon [env]
随时使用“workon 环境名”可以进行环境切换,如果不带环境名参数,则显示当前使用的环境
- deactivate
在某个环境中使用,切换到系统的python环境

其他命令
- showvirtualenv [env] 显示指定环境的详情。
- rmvirtualenv [env] 移除指定的虚拟环境,移除的前提是当前没有在该环境中工作。如在该环境工作,先使用deactivate退出。
- cpvirtualenv [source] [dest] 复制一份虚拟环境。
- cdvirtualenv [subdir] 把当前工作目录设置为所在的环境目录。
- cdsitepackages [subdir] 把当前工作目录设置为所在环境的sitepackages路径。
- add2virtualenv [dir] [dir] 把指定的目录加入当前使用的环境的path中,这常使用于在多个project里面同时使用一个较大的库的情况。
- toggleglobalsitepackages -q 控制当前的环境是否使用全局的sitepackages目录。
Virtualenvwrapper is a tool that simplifies the management of Python virtual environments in Linux. It provides a set of commands to create, activate, and delete virtual environments, making it easier to work with different Python projects with their own dependencies. To use Virtualenvwrapper, you need to have Python and Virtualenv installed on your system. Here are the steps to set up Virtualenvwrapper on Linux: 1. Install Virtualenv: ``` $ pip install virtualenv ``` 2. Install Virtualenvwrapper: ``` $ pip install virtualenvwrapper ``` 3. Add the following lines to your shell startup file (e.g., `~/.bashrc` or `~/.bash_profile`): ``` export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 # Path to your Python interpreter export WORKON_HOME=~/.virtualenvs # Directory to store virtual environments source /usr/local/bin/virtualenvwrapper.sh # Path to virtualenvwrapper.sh ``` 4. Reload the shell startup file: ``` $ source ~/.bashrc ``` 5. Create a virtual environment: ``` $ mkvirtualenv myenv ``` 6. Activate the virtual environment: ``` $ workon myenv ``` Now you can install Python packages within the virtual environment without affecting your system-wide Python installation. You can also switch between different virtual environments using the `workon` command. Remember, Virtualenvwrapper is just a wrapper around Virtualenv, so it uses the same underlying mechanisms for creating and managing virtual environments. It provides additional convenience commands and organizes virtual environments in a more structured way.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值