目录
CentOS Linux 自带 Python 2 安装virtualenv
Python3 安装虚拟环境
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel mysql-devel libffi-devel gcc make
#python 3.8.12
wget https://www.python.org/ftp/python/3.7.8/Python-3.8.12.tgz
#将python安装到python3.8⽬录,与系统原有的不相⼲,同时打开sqlite扩展
tar -xvf Python-3.8.12.tgz
cd Python-3.8.12/
./configure --prefix=/usr/local/python3.8.12 --enable-loadable-sqlite-extensions
make
make install
ln -s /usr/local/python3.8.12 /opt/app/python3.8.12
#安装venv 推荐
mkdir /export/app/venv/
cd /export/app/venv/
/opt/app/python3.8.12/bin/python3 -m venv object
#安装 virtualenv
/opt/app/python3.8.12/bin/pip3 install virtualenv
/opt/app/python3.8.12/bin/virtualenv /export/app/venv/object --python=/usr/local/python3.8.12/bin/python3
CentOS Linux 自带 Python 2 安装virtualenv
#当前版本
[root@localhost bin]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@localhost bin]# python -V
Python 2.7.5
一般方法是 pip install virtualenv -i https://mirrors.aliyun.com/pypi/simple/, 一步到位。
有可能还需要 yum install python-pip 装一下pip
安装发生报错
#pip 安装
[root@localhost venv]# pip install virtualenv -i https://mirrors.aliyun.com/pypi/simple/
Collecting virtualenv
Downloading https://mirrors.aliyun.com/pypi/packages/56/a2/3e5fdac9ecca6a3a6d2f63f7a486afd4a72728ba9f2ae83fa43f7af8ac8b/virtualenv-20.13.2-py2.py3-none-any.whl (8.7MB)
100% |████████████████████████████████| 8.7MB 96kB/s
Collecting importlib-metadata>=0.12; python_version < "3.8" (from virtualenv)
Downloading https://mirrors.aliyun.com/pypi/packages/73/0f/def168c6162596051dcc6acaffc4984ec742eb0c79ce02e51ddc11772b1c/importlib_metadata-4.11.2.tar.gz (43kB)
100% |████████████████████████████████| 51kB 360kB/s
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
IOError: [Errno 2] No such file or directory: '/tmp/pip-build-1B_rZv/importlib-metadata/setup.py'
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-1B_rZv/importlib-metadata/
#easy_install
[root@localhosts venv]# easy_install virtualenv
Searching for virtualenv
Reading https://pypi.python.org/simple/virtualenv/
Couldn't find index page for 'virtualenv' (maybe misspelled?)
Scanning index of all packages (this may take a while)
Reading https://pypi.python.org/simple/
No local packages or download links found for virtualenv
error: Could not find suitable distribution for Requirement.parse('virtualenv')
应该模块版本不兼容,不过很多模块都加入 yum 源,变成 python-XXX模式
[root@loclahost python]# yum list |grep virtualenv
python-virtualenv.noarch 15.1.0-5.el7_9 @updates
就是它了,接下来顺顺利利安装完成
#安装
yum install python-virtualenv python-devel
#创建一个虚拟环境
/usr/bin/virtualenv /export/app/venv/object --python=/usr/bin/python
#切换到虚拟环境
cd /export/app/venv/object
source bin/activate
#安装项目依赖
pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
#退出虚拟环境
deactivate
另外这种方式也适应与python3 等其他版本
/usr/bin/virtualenv /export/app/venv/test --python=/opt/app/python3.8.12/bin/python3
不过python3有自带的venv模块。更方便更稳定。
希望能帮助各位减少运维时间