安装及创建python虚拟环境
阅读原文时间:2023年07月13日阅读:3

有点气,是真的有点气,以为安装错误了,没想到是命令问题

参考链接:

https://cloud.tencent.com/developer/article/1176291

https://www.cnblogs.com/belic/p/python_virtualenv.html

https://www.cnblogs.com/zdz8207/p/3793246.html

首先,我是用子用户安装的,发现没有权限,开通权限安装好了,最后还是要root才能创建虚拟环境,所以我觉得可以一开始就用root安装

背景是阿里云端,centos7系统

先说明,因为之前安装过其他包,openssl已经装过了,pip也更新过了

pip3 install virtualenv

pip3 install virtualenvwrapper

mkdir /virtualenvs

遇到了一个问题。home/wang目录下不能创建文件夹,说是permission denied

对当前用户增加读写权限,对于目录/home/wang

chmod +wx /home/wang

linux还需要安装一个包,否则找不到virtualenvwrapper.sh文件

yum install python-virtualenv -y

这是原文说的,但我并没有出现这个问题

首选找到对应的virtualenvwrapper.sh文件的路径:

[root@localhost ~]# find / -name virtualenvwrapper.sh
/usr/local/python3/bin/virtualenvwrapper.sh

我的位置也是一样

找到路径之后在/etc/profile中进行配置:

vim /etc/profile

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

第一行:virtualenvwrapper存放虚拟环境目录

第二行:virtrualenvwrapper会安装到python的bin目录下,所以该路径是python安装目录下

读入配置文件,立即生效

[root@localhost virtualenvs]# source /etc/profile

报错:

/usr/bin/python: No module named virtualenvwrapper

找到这个文件:vim /usr/local/python3/bin/virtualenvwrapper.sh

然后修改

➜ ~ vim /usr/local/bin/virtualenvwrapper.sh …

47 # Locate the global Python where virtualenvwrapper is installed.

48 if [ "${VIRTUALENVWRAPPER_PYTHON:-}" = "" ]

49 then

50 VIRTUALENVWRAPPER_PYTHON="$(command \which python)"

51 fi

第50行,"$(command \which python)"改成"$(command \which python3)"

把第50行最后的which python 改成which python3

然后我创建虚拟环境 mkvirtualenv

cd /virtualenvs

mkvirtualenv venv1

显示:

[wang@izm5e47qpndp7jfqjxvf13z virtualenvs]$ source /etc/profile
virtualenvwrapper.user_scripts creating /home/virtualenvs/premkproject
virtualenvwrapper.user_scripts creating /home/virtualenvs/initialize

[wang@izm5e47qpndp7jfqjxvf13z virtualenvs]$ mkvirtualenv --python=/usr/local/python3/bin/python3.5 venv1
Already using interpreter /usr/local/python3/bin/python3.5
Using base prefix '/usr/local/python3'
Traceback (most recent call last):
File "/usr/bin/virtualenv", line 10, in
sys.exit(main())
File "/usr/local/python3/lib/python3.5/site-packages/virtualenv.py", line 793, in main
symlink=options.symlink,
File "/usr/local/python3/lib/python3.5/site-packages/virtualenv.py", line 1071, in create_environment
install_python(home_dir, lib_dir, inc_dir, bin_dir, site_packages=site_packages, clear=clear, symlink=symlink)
File "/usr/local/python3/lib/python3.5/site-packages/virtualenv.py", line 1344, in install_python
mkdir(lib_dir)
File "/usr/local/python3/lib/python3.5/site-packages/virtualenv.py", line 389, in mkdir
os.makedirs(at_path)
File "/usr/local/python3/lib/python3.5/os.py", line 231, in makedirs
makedirs(head, mode, exist_ok)
File "/usr/local/python3/lib/python3.5/os.py", line 231, in makedirs
makedirs(head, mode, exist_ok)
File "/usr/local/python3/lib/python3.5/os.py", line 241, in makedirs
mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/home/virtualenvs/venv1'

行吧,这里的权限怎么调都不对,都是deny,那我换成root好吧

但是!不管怎么着,我就是报错说mkvirtualenv这个命令不可用

cd virtualenvs
virtualenv --no-site-packages venv1

启用虚拟环境

cd venv1
source bin/activate

(venv) [wang@izm5e47qpndp7jfqjxvf13z venv]$

吃瓜吃瓜