2022-6-26 : Centos7 安装虚拟环境 virtualenv

本文详细介绍了如何在Python3环境中安装virtualenv和virtualenvwrapper,配置环境变量,创建、管理和删除虚拟环境,并演示了常用命令。重点在于解决多版本Python环境下虚拟环境设置的问题。
摘要由CSDN通过智能技术生成

安装 virtualenv

[root@bogon j]# 
[root@bogon j]# python3 -m pip install virtualenv -i https://mirrors.aliyun.com/pypi/simple/
...

安装 virtualenv 管理工具 virtualenvwrapper

[root@bogon j]# 
[root@bogon j]# 
[root@bogon j]# 
[root@bogon j]# python3 -m pip install virtualenvwrapper -i https://mirrors.aliyun.com/pypi/simple/
...
[root@bogon j]# 
[root@bogon j]# 

## 创建虚拟环境文件夹

[root@bogon j]# mkdir .virtualenvs
[root@bogon j]# 
[root@bogon j]# 

## 查看安装位置

[root@bogon j]# 
[root@bogon j]# whereis virtualenv
virtualenv: /usr/local/bin/virtualenv
[root@bogon j]# 
[root@bogon j]# 

## 修改配置文件  添加后面带注释的4行

[root@bogon j]# vim ~/.bashrc
[root@bogon j]# 
[root@bogon j]# cat ~/.bashrc
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi


export WORKON_HOME=/home/j/.virtualenvs  # 虚拟环境文件位置

source /usr/local/bin/virtualenvwrapper.sh  # 虚拟环境管理工具位置

export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv  # 虚拟环境安装位置

VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3  # Python3 解释器位置

[root@bogon j]# 
[root@bogon j]# 
[root@bogon j]# 

## 重新加载源文件

[root@bogon j]# 
[root@bogon j]# source ~/.bashrc
/bin/python: No module named virtualenvwrapper
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=/bin/python and that PATH is
set properly.
[root@bogon j]#

上面报错是因为,本地有两个Python环境
Python代表Python2
Python3才是Python3
下面修改一下这里

image

把python改为python3

## 查看管理工具 配置文件路径

[root@bogon j]# whereis virtualenvwrapper.sh
virtualenvwrapper: /usr/local/bin/virtualenvwrapper.sh
[root@bogon j]# 
[root@bogon j]# 
[root@bogon j]# 

## 把python改为python3

[root@bogon j]# 
[root@bogon j]# vim /usr/local/bin/virtualenvwrapper.sh
[root@bogon j]# 
[root@bogon j]# 

## 重新加载源文件

[root@bogon j]# source ~/.bashrc
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/get_env_details
[root@bogon j]# 
[root@bogon j]#

常用命令

##创建虚拟环境

[root@bogon j]# mkvirtualenv test
created virtual environment CPython3.6.8.final.0-64 in 378ms
  creator CPython3Posix(dest=/home/j/.virtualenvs/test, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
    added seed packages: pip==21.3.1, setuptools==59.6.0, wheel==0.37.1
  activators BashActivator,CShellActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/test/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/test/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/test/bin/preactivate
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/test/bin/postactivate
virtualenvwrapper.user_scripts creating /home/j/.virtualenvs/test/bin/get_env_details
(test) [root@bogon j]# 
(test) [root@bogon j]# 
(test) [root@bogon j]# 
(test) [root@bogon j]# 
(test) [root@bogon j]# 

## 退出虚拟环境

(test) [root@bogon j]# deactivate
[root@bogon j]# 
[root@bogon j]# 

## 列出虚拟环境

[root@bogon j]# workon
test
[root@bogon j]# 

## 进入虚拟环境

[root@bogon j]# workon test
(test) [root@bogon j]# 
(test) [root@bogon j]# 

## 列出虚拟环境
(test) [root@bogon j]# lsvirtualenv
test
====


(test) [root@bogon j]# 
(test) [root@bogon j]# 
(test) [root@bogon j]# 
(test) [root@bogon j]# deactivate
[root@bogon j]# 
[root@bogon j]# 
[root@bogon j]# 

## 删除虚拟环境

[root@bogon j]# rmvirtualenv test
Removing test...
[root@bogon j]# 
[root@bogon j]# 
[root@bogon j]# lsvirtualenv
[root@bogon j]# 
[root@bogon j]# workon
[root@bogon j]# 
[root@bogon j]# 
[root@bogon j]#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JFLEARN

CSDN这么拉会有人打赏?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值