Ubuntu上搭建虚拟环境

环境搭建

1. 安装pip

apt install python3-pip

2.安装虚拟环境命令

pip 安装可能超时,可以使用一下国内镜像

临时使用方法只需要在pip命令上添加:–index-url https://pypi.douban.com/simple

使用如下方式全局添加:
mkdir ~/.pip
#创建pip.conf
vim ~/.pip/pip.conf
#添加内容
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

2.1 安装virtualenv

root@dell-PowerEdge-R740:~# pip install virtualenv
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting virtualenv
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/86/2f/35a79942c38d4ca89b444085d67900f713f1967446fdcefc8351619d7c65/virtualenv-20.21.0-py3-none-any.whl (8.7 MB)
     |████████████████████████████████| 8.7 MB 1.4 MB/s 
Collecting filelock<4,>=3.4.1
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/9e/6b/fdcd53aeee771a868c4187f0955116894a2b1e82d73fb5990c2ef63afc18/filelock-3.11.0-py3-none-any.whl (10.0 kB)
Collecting platformdirs<4,>=2.4
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/b2/f3/4fb5fae710fc9f22a42cd90dc0547da18ec83e2e139294ab94f04c449cf5/platformdirs-3.2.0-py3-none-any.whl (14 kB)
Collecting distlib<1,>=0.3.6
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/76/cb/6bbd2b10170ed991cf64e8c8b85e01f2fb38f95d1bc77617569e0b0b26ac/distlib-0.3.6-py2.py3-none-any.whl (468 kB)
     |████████████████████████████████| 468 kB 45.7 MB/s 
Installing collected packages: filelock, platformdirs, distlib, virtualenv
Successfully installed distlib-0.3.6 filelock-3.11.0 platformdirs-3.2.0 virtualenv-20.21.0

2.2 安装virtualenvwrapper

root@dell-PowerEdge-R740:~# pip install virtualenvwrapper
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting virtualenvwrapper
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c1/6b/2f05d73b2d2f2410b48b90d3783a0034c26afa534a4a95ad5f1178d61191/virtualenvwrapper-4.8.4.tar.gz (334 kB)
     |████████████████████████████████| 334 kB 912 kB/s 
Collecting stevedore
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/41/1b/bad9124b400334d48aed1e04799032909fd8f1ca4a8e0eb30841284dd489/stevedore-5.0.0-py3-none-any.whl (49 kB)
     |████████████████████████████████| 49 kB 5.8 MB/s 
Requirement already satisfied: virtualenv in /usr/local/lib/python3.8/dist-packages (from virtualenvwrapper) (20.21.0)
Collecting virtualenv-clone
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/21/ac/e07058dc5a6c1b97f751d24f20d4b0ec14d735d77f4a1f78c471d6d13a43/virtualenv_clone-0.5.7-py3-none-any.whl (6.6 kB)
Collecting pbr!=2.1.0,>=2.0.0
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/01/06/4ab11bf70db5a60689fc521b636849c8593eb67a2c6bdf73a16c72d16a12/pbr-5.11.1-py2.py3-none-any.whl (112 kB)
Requirement already satisfied: platformdirs<4,>=2.4 in /usr/local/lib/python3.8/dist-packages (from virtualenv->virtualenvwrapper) (3.2.0)
Requirement already satisfied: distlib<1,>=0.3.6 in /usr/local/lib/python3.8/dist-packages (from virtualenv->virtualenvwrapper) (0.3.6)
Requirement already satisfied: filelock<4,>=3.4.1 in /usr/local/lib/python3.8/dist-packages (from virtualenv->virtualenvwrapper) (3.11.0)
Building wheels for collected packages: virtualenvwrapper
  Building wheel for virtualenvwrapper (setup.py) ... done
  Created wheel for virtualenvwrapper: filename=virtualenvwrapper-4.8.4-py2.py3-none-any.whl size=24833 sha256=bf86ddff2b764e8647692e097c462c217acb2a71b4c9840004c476b355232e10
  Stored in directory: /root/.cache/pip/wheels/97/88/d8/a01a6ba56275a7bfbf0118f79c81de4235f2dd05c1d4ad71cc
Successfully built virtualenvwrapper
Installing collected packages: pbr, stevedore, virtualenv-clone, virtualenvwrapper
Successfully installed pbr-5.11.1 stevedore-5.0.0 virtualenv-clone-0.5.7 virtualenvwrapper-4.8.4

2.3找不到mkvirtualenv解决办法

在用户家下的.bashrc文件中添加如下代码:

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

执行source .bashrc

dell@dell-PowerEdge-R740:~$ source .bashrc 

Command '' not found, but can be installed with:

sudo apt install mailutils-mh  # version 1:3.7-2.1, or
sudo apt install meshio-tools  # version 4.0.4-1
sudo apt install mmh           # version 0.4-2
sudo apt install nmh           # version 1.7.1-6
sudo apt install termtris      # version 1.3-1

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.

如果想上面那样报错,解决办法是找到virtualenvwrapper.sh文件修改代码。
查看这个文件到位置

dell@dell-PowerEdge-R740:~$ whereis virtualenvwrapper.sh
virtualenvwrapper: /usr/local/bin/virtualenvwrapper.sh

找到如下代码进行修改。
修改前:

# Locate the global Python where virtualenvwrapper is installed.
if [ "${VIRTUALENVWRAPPER_PYTHON:-}" = "" ]
then
    # 这里需要修改
    VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
fi

# Set the name of the virtualenv app to use.
if [ "${VIRTUALENVWRAPPER_VIRTUALENV:-}" = "" ]
then
    VIRTUALENVWRAPPER_VIRTUALENV="virtualenv"
fi

修改后:

# Locate the global Python where virtualenvwrapper is installed.
if [ "${VIRTUALENVWRAPPER_PYTHON:-}" = "" ]
then
    # 这里已被修改
    VIRTUALENVWRAPPER_PYTHON="$(command \which python3)"
fi

# Set the name of the virtualenv app to use.
if [ "${VIRTUALENVWRAPPER_VIRTUALENV:-}" = "" ]
then
    VIRTUALENVWRAPPER_VIRTUALENV="virtualenv"
fi

再次执行source .bashrc打印如下表示更新成功。

dell@dell-PowerEdge-R740:~$ source .bashrc 
virtualenvwrapper.user_scripts creating /home/dell/.virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /home/dell/.virtualenvs/postmkproject
virtualenvwrapper.user_scripts creating /home/dell/.virtualenvs/initialize
virtualenvwrapper.user_scripts creating /home/dell/.virtualenvs/premkvirtualenv
virtualenvwrapper.user_scripts creating /home/dell/.virtualenvs/postmkvirtualenv
virtualenvwrapper.user_scripts creating /home/dell/.virtualenvs/prermvirtualenv
virtualenvwrapper.user_scripts creating /home/dell/.virtualenvs/postrmvirtualenv
virtualenvwrapper.user_scripts creating /home/dell/.virtualenvs/predeactivate
virtualenvwrapper.user_scripts creating /home/dell/.virtualenvs/postdeactivate
virtualenvwrapper.user_scripts creating /home/dell/.virtualenvs/preactivate
virtualenvwrapper.user_scripts creating /home/dell/.virtualenvs/postactivate
virtualenvwrapper.user_scripts creating /home/dell/.virtualenvs/get_env_details

虚拟环境命令的使用

1. 创建虚拟环境

Linux上使用mkvirtualenv命令创建虚拟环境。在不指定Python版本下,默认使用Python2创建。

  • 在Python2下创建
mkvirtualenv 虚拟环境名称
  • 在Python3下创建
mkvirtualenv -p python3 虚拟环境名称

示例:
1.png
创建成功后,会自动进入创建的虚拟环境中。
提示:

  • 创建虚拟环境需要联网
  • 创建成功后, 会自动工作在这个虚拟环境上
  • 工作在虚拟环境上, 提示符最前面会出现 “虚拟环境名称”

2. 查看虚拟环境

使用workon命令查看虚拟环境。

workon 

示例:
image.png

3. 使用虚拟环境

使用workon 虚拟环境命令进入虚拟环境。

workon 虚拟环境命令

示例:
image.png

4. 退出虚拟环境

使用deactivate命令退出虚拟环境。

deactivate

示例:
image.png

5. 删除虚拟环境

使用rmvirtualenv命令删除虚拟环境。

rmvirtualenv 虚拟环境名称

image.png

参考文档

  1. https://blog.csdn.net/m0_61491995/article/details/125960379
  • 13
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值