阿里云Linux系统配置python3-虚拟环境-mysql --踩坑实践(Ubuntu系统转centOS7)


序言:个人使用阿里云的服务器,Ubuntu系统时在创建虚拟环境处卡住,多次修改无果后改用CentOS系统配置环境成功,但是其中也是尝试多次,这里就把自己找的能够配置成功的方法整理起来,以备后续有相关配置时,能够快速应对。


一、Ubuntu系统配置(mysql --virtualenv

1. mysql安装

平时使用虚拟机时,基本没有太多涉及到这样的问题。就是安装前先apt-git命令升级下

sudo apt-get update
sudo apt-get install mysql-server

注:Ubuntu 最新系统自带python3不存在安装python部分,所以直接跳转到虚拟环境的安装。

2. 安装虚拟环境

安装虚拟环境报如下错误:

root@iZ2zef57ni8z6z1jo0l6flZ:/home# virtualenv django_news
Running virtualenv with interpreter /usr/bin/python2
New python executable in /home/django_news/bin/python2
Also creating executable in /home/django_news/bin/python
Please make sure you remove any previous custom paths from your /root/.pydistutils.cfg file.
Installing setuptools, pkg_resources, pip, wheel...
  Complete output from command /home/django_news/bin/python2 - setuptools pkg_resources pip wheel:
  Collecting setuptools
  Downloading http://mirrors.cloud.aliyuncs.com/pypi/packages/66/e8/570bb5ca88a8bcd2a1db9c6246bb66615750663ffaaeada95b04ffe74e12/setuptools-40.2.0-py2.py3-none-any.whl (568kB)
Collecting pkg_resources
Exception:
Traceback (most recent call last):
  File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/basecommand.py", line 209, in main
    status = self.run(options, args)
  File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/commands/install.py", line 328, in run
    wb.build(autobuilding=True)
  File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/wheel.py", line 748, in build
    self.requirement_set.prepare_files(self.finder)
  File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/req/req_set.py", line 360, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/req/req_set.py", line 512, in _prepare_file
    finder, self.upgrade, require_hashes)
  File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/req/req_install.py", line 273, in populate_link
    self.link = finder.find_requirement(self, upgrade)
  File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/index.py", line 442, in find_requirement
    all_candidates = self.find_all_candidates(req.name)
  File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/index.py", line 400, in find_all_candidates
    for page in self._get_pages(url_locations, project_name):
  File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/index.py", line 545, in _get_pages
    page = self._get_page(location)
  File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/index.py", line 648, in _get_page
    return HTMLPage.get_page(link, session=self.session)
  File "/usr/share/python-wheels/pip-8.1.1-py2.py3-none-any.whl/pip/index.py", line 760, in get_page
    resp.raise_for_status()
  File "/home/django_news/share/python-wheels/requests-2.9.1-py2.py3-none-any.whl/requests/models.py", line 840, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
HTTPError: 404 Client Error: Not Found for url: http://mirrors.cloud.aliyuncs.com/pypi/simple/pkg-resources/
----------------------------------------
...Installing setuptools, pkg_resources, pip, wheel...done.
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/virtualenv.py", line 2363, in <module>
    main()
  File "/usr/lib/python3/dist-packages/virtualenv.py", line 719, in main
    symlink=options.symlink)
  File "/usr/lib/python3/dist-packages/virtualenv.py", line 988, in create_environment
    download=download,
  File "/usr/lib/python3/dist-packages/virtualenv.py", line 918, in install_wheel
    call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT)
  File "/usr/lib/python3/dist-packages/virtualenv.py", line 812, in call_subprocess
    % (cmd_desc, proc.returncode))
OSError: Command /home/django_news/bin/python2 - setuptools pkg_resources pip wheel failed with error code 2

解决办法:
查了很多资料,有部分网友说是pip源的问题,需要修改/root/.pip/pip.conf的配置文件,将其优化为如下代码:

[global]
#index-url=http://mirrors.cloud.aliyuncs.com/pypi/simple/
index-url=http://e.pypi.python.org/simple
[install]
trusted-host=mirrors.cloud.aliyuncs.com

实际执行后,进而报如下错误:

root@iZ2zef57ni8z6z1jo0l6flZ:/home/study/django# virtualenv -p python3 django_news
Already using interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in /home/study/django/django_news/bin/python3
Also creating executable in /home/study/django/django_news/bin/python
Please make sure you remove any previous custom paths from your /root/.pydistutils.cfg file.
Installing setuptools, pkg_resources, pip, wheel...done.

以上方法都不奏效,所以换为CentOS系统进行配置。


二、CentOS系统配置(python3--mkvirtualenv -- mysql


1. python3的安装及配置

因为centos系统默认是不安装python3的所以这里如果用到python3的同学需要先安装。
python3安装配置部分,已经被大家写了很多了。所以这里不再累述。我找到了一篇这方面的博文,写的非常详细,大家可以按照这个教程进行安装。文章链接如下:

----- Centos 7安装python3

请注意:因为很多的centos系统中是没有安装zlibzlib-devel这两个包的,所以在安装python3前,请先安装这两个包,使安装python3时关联这两个库。否则后面创建虚拟环境命令部分会报如下错误:
ps:不要问我怎么知道的,填坑的经历不忍回忆。

mkvirtualenv env3 --python=python3

Traceback (most recent call last):
File "/usr/lib/python3/bin/virtualenv.py", line 17, in <module>
import zlib
ImportError: No module named zlib

zlibzlib-devel模块的安装命令如下:

yum install -y zlib zlib-devel

2. 虚拟环境的安装

在保证上述的python3顺利安装完成后,开始进行接下来的虚拟环境的安装。安装步骤如下:

  • 安装virtualenv virtualenvwrapper
pip install virtualenv virtualenvwrapper 
  • 如果没有pip 先安装
pip:yum install -y pip 
  • 安装好了之后,查找virtualenv位置:
find / -name virtualenv 
例:返回:/usr/local/python3/bin/virtualenv 
查找virtualenvwrapper.sh位置:find / -name virtualenvwrapper.sh 
例:返回:/usr/local/python3/bin/virtualenvwrapper.sh
  • 在/usr/bin中添加环境变量(就是新建了一个软连接) (如果bin文件下没有的话,有就可以忽略这一步)
ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv
  • 修改环境变量相关脚本/root/.bashrc
    添加如下代码
export WORKON_HOME=/root/.virtualenvs#创建一个存放虚拟环境的目录 
export PROJECT_HOME=/root/workspace 
source /usr/local/python3/bin/virtualenvwrapper.sh #virtualenvwrapper.sh的位置 
  • 然后重启:source /.bashrc
  • 检验
    输入:mkvirtualenv 查看出现的信息,如果提示命令不存在,那么就是没有安装成功
    基本命令:
    mkvirtualenv test #创建一个虚拟环境默认指向python3
    workon test #进入test虚拟环境
    deactivate #退出虚拟环境

配置完虚拟环境后,心里大爽啊,毕竟之前就是Ubuntu的虚拟环境中花了很多时间。不多说了,抓紧时间配置mysql吧。


三、安装mysql

这一类的文章也是很多了,但是我通过尝试了.gz安装包,.rpm安装包,发现后者更加简单,前者或多或少的存在这样或那样的问题,所以,这里我推荐一篇自己经过筛选实践的文章给大家。按照这个来安装简单方便。

阿里云服务器Linux(CentOS)中MySQL5.7安装以及完整的配置

希望大家能够快速搭建基本的环境,少花些时间在环境的配置上,有更多的时间用来提升自己!


四、安装一些包时,出现的报错情况记录


1.报错python版本与安装包不匹配

报错信息如下:

    aliyun-python-sdk-core-v3 requires Python '>=3' but the running Python is 2.7.5

先使用find / -name pip3

        /usr/local/python3/bin/pip3

如果存在的话,我们进入usr/bin目录下,使用 ll pip* 命令查看下,pip都有哪些;这里会显示没有pip3命令。所以我们需要将pip3pip3的目录连接起来:

    ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

这样我们就可以使用pip3 install xxx 安装需要python3才能安装的包了。


2.安装mysqlclient等类似ORM连接的包报错

报错信息如下:

(django_news) [root@iZ2zef57ni8z6z1jo0l6flZ django_news]# pip3 install mysqlclient
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting mysqlclient
  Downloading http://mirrors.aliyun.com/pypi/packages/ec/fd/83329b9d3e14f7344d1cb31f128e6dbba70c5975c9e57896815dbb1988ad/mysqlclient-1.3.13.tar.gz (90kB)
    100% |████████████████████████████████| 92kB 56.9MB/s 
    Complete output from command python setup.py egg_info:
    /bin/sh: mysql_config: command not found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-w80_hk2h/mysqlclient/setup.py", line 18, in <module>
        metadata, options = get_config()
      File "/tmp/pip-install-w80_hk2h/mysqlclient/setup_posix.py", line 53, in get_config
        libs = mysql_config("libs_r")
      File "/tmp/pip-install-w80_hk2h/mysqlclient/setup_posix.py", line 28, in mysql_config
        raise EnvironmentError("%s not found" % (mysql_config.path,))
    OSError: mysql_config not found
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-w80_hk2h/mysqlclient/

解决办法:需要先安装mysql-devel, 之后再安装mysqlclient包.安装命令如下:

        yum -y install mysql-devel 

3.安装阿里云的包报错

报错信息如下:

(django_news) [root@iZ2zef57ni8z6z1jo0l6flZ django_news]# pip3 install aliyun-python-sdk-dysmsapi==1.0.0
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting aliyun-python-sdk-dysmsapi==1.0.0
  Downloading http://mirrors.aliyun.com/pypi/packages/bb/6b/c66c776967dca765c8614feb94e3d20c32b1484549663db389692cb251ed/aliyun-python-sdk-dysmsapi-1.0.0.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-2hulf6co/aliyun-python-sdk-dysmsapi/setup.py", line 20, in <module>
        import ssl
      File "/usr/local/python3/lib/python3.6/ssl.py", line 101, in <module>
        import _ssl             # if we can't import it, let the error propagate
    ModuleNotFoundError: No module named '_ssl'
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-2hulf6co/aliyun-python-sdk-dysmsapi/

即说没有安装ssl模块,然而在运行python2时是可以正常导入,但是python3就报错,不能导入ssl。

[root@iZ2zef57ni8z6z1jo0l6flZ bin]# python2
Python 2.7.5 (default, Nov  6 2016, 00:28:07) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> 

这里查阅了很多资料,大部分都说名要修改python3安装包的/Modules/Setup.dist或‘/Modules/Setup’文件,都是要将两个文件中的以下部分进行修改:

注意:我这里修改的/Modules/Setup.dist文件

# 原始代码
# Socket module helper for socket(2)
#_socket socketmodule.c
 
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
#SSL=/usr/local/ssl
#_ssl _ssl.c \
#           -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
#           -L$(SSL)/lib -lssl -lcrypto

#################分割线###################

# 修改后的代码
#修改结果如下:
# Socket module helper for socket(2)
_socket socketmodule.c timemodule.c # 修改部分,去掉#号,添加timemodule.c
 
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/ssl # 去掉#号
_ssl _ssl.c \
            -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \ # 去掉#号
            -L$(SSL)/lib -lssl -lcrypto # 去掉#号

按照以上部分修改后,再一次执行如下命令:

./configure --prefix=/usr/local/python3 # 把python安装到的文件夹位置
make
make install

执行完成之后,再次运行python3,执行如下名称,查看是否成功。我这样修改后运行成功。

import ssl
# 成功导入ssl实例如下:
Python 3.6.3 (default, Sep 10 2018, 22:21:28) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>> 

ps:这个错误的每个部分的代码都仔细修改,如果还是不行的话,可以参考下面这个链接的方法试一下。
http://www.linuxdiyf.com/linux/32547.html
该方法主要区别时在安装时加入./configure ----with-ssl命令,有类似情况的可以试一下。


猜你喜欢

转载自www.cnblogs.com/achjiang/p/9899702.html