1. 程式人生 > >阿里雲Linux系統配置python3-虛擬環境-mysql --踩坑實踐(Ubuntu系統轉centOS7)

阿里雲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. 安裝虛擬環境

安裝虛擬環境報如下錯誤:

[email protected]:/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

實際執行後,進而報如下錯誤:

[email protected]:/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) [[email protected] 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) [[email protected] 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。

[[email protected] 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命令,有類似情況的可以試一下。