Ubuntu安装Virtualenvwrapper
前言
最近想要玩玩符号执行,根据angr官网的文档,推荐使用python虚拟环境搭建angr。同样,我也不想让angr污染我的python开发环境(毕竟环境变化万一出什么幺蛾子呢)。在使用mkvirtualenv命令时提示找不到命令,查了查Virtualenvwrapper,网上大多数安装教程貌似并不能解决问题(可能太久远了),所以只能自己摸索一下了。Linux环境各不相同,这里提供个思路和大致的步骤。
系统环境 | 系统环境 |
---|---|
操作系统 | Ubuntu20.04-server |
Virtualenvwrapper 简介
Virtualenvwrapper 是 Virtualenv 的扩展包,可以把新创建的环境记录下来,不需要每次启动虚拟环境时都执行一遍 source 命令,可以更方便的管理虚拟环境。
他可以帮我们:
1、将所有虚拟环境整合在一个目录下
2、管理(新增,删除,查找,复制)虚拟环境
3、简便的切换虚拟环境
安装virtualenvwrapper
pip install virtualenvwrapper
折腾的过程
安装中出现WARNING,大致是说没有将~/.local/bin
加入到PATH
。
WARNING: The script pbr is installed in '~/.local/bin' which is not on PATH.
# Add an "alert" alias for long running commands. Use like so:
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The script virtualenv is installed in '~/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The script virtualenv-clone is installed in '~/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
搜索virtualenvwrapper.sh路径
sudo find / -name virtualenvwrapper.sh
编辑~/.bashrc
vi ~/.bashrc
添加环境变量$HOME/.local/bin
,具体路径根据WARNING提示修改即可;添加virtualenvwrapper.sh执行路径,这样每次bash启动都可以source virtualenvwrapper.sh
export PATH=$PATH:$HOME/.local/bin
source /home/ldh/.local/bin/virtualenvwrapper.sh
:wq
保存后使配置文件生效
source ~/.bashrc
提示要正确设定VIRTUALENVWRAPPER_PYTHON
环境变量
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON= and that PATH is
set properly.
搜索python路径(当然,你要是知道就不用搜了呗)
sudo find / -name python3
/usr/bin/python3
编辑.bashrc文件
vi .bashrc
添加环境变量
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
:wq
保存后使配置文件生效
source .bashrc
没有报错,并且可以正常使用mkvirtualenv创建虚拟环境并运行虚拟环境。
最终需要在.bashrc文件中添加的环境变量
export PATH=$PATH:$HOME/.local/bin
# virtualenvwrapper path
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source $HOME/.local/bin/virtualenvwrapper.sh
当然路径不是每个系统都相同,用find命令找一下替换掉就行
virtualenvwrapper基本使用方法
# 创建
$ mkvirtualenv --python=$(which python3) <venv name>
# 进入
$ workon <venv name>
# 退出
$ deactivate
# 列出所有的虚拟环境,两种方法
$ workon
$ lsvirtualenv
# 在虚拟环境内直接切换到其他环境
$ workon <venv name>
# 删除虚拟环境
$ rmvirtualenv <venv name>