1. 程式人生 > >mac 部署python環境

mac 部署python環境

mac python

Python中最連接Mysql常用的驅動是

mysql-python :mysql的C語言的驅動
mysql-connector:msql官方的驅動
pymysql:python語言的驅動

我這裏安裝的是 mysql-python

終端中執行

pip install mysql-python

運氣好的的話,直接就成功了,我遇到了一下問題,最終找到了解決方案,原因及解決方案如下,供參考

問題1:mysql_config not found

終端打印出:

Collecting mysql-python
  Downloading MySQL-python-1.2.5.zip (108kB)
    100% |████████████████████████████████| 110kB 30kB/s 
    Complete output from command python setup.py egg_info:
    sh: mysql_config: command not found
    Traceback (most recent call last):      File "<string>", line 20, in <module>      File "/private/tmp/pip-build-NP8J3v/mysql-python/ setup.py", line 17, in <module>
        metadata, options = get_config()      File "setup_posix.py", line 43, in get_config
        libs = mysql_config("libs_r")      File "setup_posix.py", line 25, in mysql_config        raise EnvironmentError("%s not found" %     (mysql_config.path,))
    EnvironmentError: mysql_config not found----------------------------------------Command "python setup.py egg_info" failed with error code 1 in /private/tmp/pip-build-NP8J3v/mysql-python

解決方法,執行:

ln -s /usr/local/mysql/bin/mysql_config /usr/local/bin/mysql_config

原因
找不到mysql_config一般是由於通過lnmp.org或者其他方式安裝mysql以後mysql_config是在/usr/local/mysql/bin/裏面,這裏面的文件不是在任意位置都可以訪問的,而指令是

將mysql_config鏈接到/usr/local/bin目錄下

參考一條命令解決mysql_config not found

問題2:image not found python

出錯信息為:

Traceback (most recent call last):  File "manage.py", line 4, in <module>    from  models import User
  File "/Users/tengfei/PycharmProjects/mysql/models.py", line 1, in <module>    import  MySQLdb  File "/Library/Python/2.7/site-packages/MySQLdb/__init__.py", line 19, in <module>    import _mysql
ImportError: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib  Referenced from: /Library/Python/2.7/site-packages/_mysql.so
  Reason: image not found

解決:

pip install --upgrade pip

修改 OS X 環境變量:打開終端,在終端中使用 vim 打開 “~/.bash_profile”,如果沒有安裝 vim,那就顯示隱藏文件用文本編輯器打開,具體操作這裏就不復述了。在 .bash_profile 中添加以下內容

PATH="/usr/local/mysql/bin:${PATH}"export PATHexport DYLD_LIBRARY_PATH=/usr/local/mysql/lib/export VERSIONER_PYTHON_PREFER_64_BIT=noexport VERSIONER_PYTHON_PREFER_32_BIT=yes

其中 VERSIONER_PYTHON_PREFER_64_BIT 和 VERSIONER_PYTHON_PREFER_64_BIT 根據自己安裝的 MySQL 進行選擇。

參考:

MySQLdb(即 MySQL-python 包)在 OS X 中安裝指南

Solving Library not loaded: libmysqlclient.18.dylib when importing MySQLdb on Lion

Mac OS + MySQL-python-1.2.3: ImportError | Library not loaded | libmysqlclient.16.dylib

問題3:command ‘x86_64-linux-gnu-gcc‘ failed with exit status 1 異常解決

執行pip install mysql-python後的出錯信息如下:
這是在虛擬環境中見到的
^
compilation terminated.
error: command ‘x86_64-linux-gnu-gcc‘ failed with exit status 1

    ----------------------------------------
Command "/home/tengfei/api01/test/TestTo/TODO-orm/venv/bin/python -u -c "import setuptools, tokenize;__file__=‘/tmp/pip-build-w6mp1W/mysql-python/setup.py‘;exec(compile(getattr(tokenize, ‘open‘, open)(__file__).read().replace(‘\r\n‘, ‘\n‘), __file__, ‘exec‘))" install --record /tmp/pip-jG7EKf-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/tengfei/api01/test/TestTo/TODO-orm/venv/include/site/python2.7/mysql-python" failed with error code 1 in /tmp/pip-build-w6mp1W/mysql-python/

原因是:一些依賴包沒有安裝,註意這也是很多實用pip執行安裝擴展的時候,常見的錯誤

apt-get build-dep python-lxml

sudo pip install lxml --upgradepip install mysql-python

參考:[Ubuntu/pip] 解決 pip 安裝 lxml 出現 x86_64-linux-gnu-gcc 異常





Mac下安裝第三方模塊報錯:‘sqlfront.h‘ file not found的解決辦法

1.軟件環境:

    mac環境:10.11.6(15G31)

    python: 3.6

2.問題:

    sudo pip install pymssql 後出現下面問題:

    fatal error: ‘sqlfront.h‘ file not found

3.解決方案:

    終端執行 brew install [email protected]

    終端執行 brew link --force [email protected]

    終端執行 sudo pip install pymssql(或則 pycharm直接安裝)



mac 部署python環境