Python virtual environment migration-solve the problem and error summary of docker deployment python environment

First of all I use the old version to create a virtual environment

首先安装虚拟环境,命令如下:

sudo pip3 install virtualenv #安装虚拟环境
接下来还要安装虚拟环境扩展包,命令如下:

sudo pip3 install virtualenvwrapper
安装虚拟环境包装器的目的是使用更加简单的命令来管理虚拟环境。

修改用户家目录下的配置文件.bashrc,添加如下内容:

export WORKON_HOME=$HOME/.virtualenvs  # 配置虚拟环境的保存位置 $HOME是用户的主目录 也就是 cd ~  这个文件夹
source /usr/local/bin/virtualenvwrapper.sh  #  配置virtualenvwrapper命令的脚本
使用source .bashrc命令使配置文件生效。
  • It is very likely to report an error when executing the above file
在docker中安装 虚拟环境时执行
source /usr/local/bin/virtualenvwrapper.sh

报错:
/usr/local/bin/virtualenvwrapper.sh: line 230: : command not found
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.
  • Reason: Because there is the following code in virtualenvwrapper.sh

# Locate the global Python where virtualenvwrapper is installed.
if [ "$VIRTUALENVWRAPPER_PYTHON" = "" ] then
    VIRTUALENVWRAPPER_PYTHON="$(command \which python)"

# 脚本会默认使用python2环境,但是virtualenvwrapper装在了python3环境中,所以会有上面的报错
  • Solution: Search the file path of local python3 directly
root@huangdonglin-virtual-machine:/usr/local/bin# find / -name python3
/root/.virtualenvs/django_py3_1.11/bin/python3
/etc/python3
/usr/lib/python3
/usr/bin/python3
/usr/share/python3
/usr/share/doc/python3
/usr/share/lintian/overrides/python3
root@huangdonglin-virtual-machine:/usr/local/bin#

直接将VIRTUALENVWRAPPER_PYTHON默认值修改为/usr/bin/python3即可,然后再次执行命令就不会报错了

Then install the virtual environment in the same way in docker

  • After we run the program directly in the local virtual environment, we can pull the entire environment into docker to prevent errors. Please see:
    Insert picture description here
    Insert picture description here
    Insert picture description here
  • Senior's answer:
    Insert picture description here

Then start writing dockerfile

  • The main idea is to package our entire virtual environment all the way to the corresponding docker directory.
    Insert picture description here
    We move this environment package to the same virtual environment directory as docker,
    but we will find that pip and python in the virtual environment cannot be used, don’t worry, Need to change it. Just
    Insert picture description here
    Insert picture description here
    change the path corresponding to these two locations to be correct!
    These two files are pip and activate respectively
    .

Guess you like

Origin blog.csdn.net/pythonstrat/article/details/108649715