gpt4 book ai didi

python - Python 虚拟环境中没有名为 MySqlDb 的模块

转载 作者:太空宇宙 更新时间:2023-11-03 10:45:23 25 4
gpt4 key购买 nike

我之前曾发布过有关此错误的信息,但需要对此进行更多说明。我目前正在 Windows 10 机器(运行 Python3.4)上使用 Visual Studio 2013 构建 Django Web 应用程序。在开始的时候,我一直在处理 MySQL 连接问题,为此我做了一个 mysqlclient pip-install。我创建了两个使用 MySQL 作为后端的项目,在安装 mysqlclient 之后,我能够通过我正在处理的当前项目连接到数据库。当我打开第二个项目并尝试连接到数据库时,我得到了同样的“No Module called MySqlDB”错误。现在,这两个项目之间的区别在于第一个不是在虚拟环境中创建的,而第二个是。

所以我推断在Python虚拟环境中创建的项目无法连接到数据库。这里有人可以帮我解决这个问题吗?我需要知道如何将 mysqlclient 模块加载到虚拟环境中,以便项目可以使用它。谢谢

最佳答案

So I have come to deduce that projects created within the Python virtual environment are not able to connect to the database

鉴于 virtualenv 是 Web 应用程序部署的事实标准,这将是非常令人惊讶的(提示:我们使用 virtualenvs 维护和托管数十个 Django 项目)。

Can someone here please help me in getting this problem solved. I need to know how the mysqlclient module can be loaded onto a virtual environment so that a project can use it

与任何 python 包相同的方式:创建你的 venv(如果没有完成),激活它并 pip 安装包:

bruno@bigb:~/Work/playground$ virtualenv venv 
New python executable in venv/bin/python
Installing distribute.............................................................................................................................................................................................done.
Installing pip...............done.
bruno@bigb:~/Work/playground$ source venv/bin/activate
(venv)bruno@bigb:~/Work/playground$ pip install mysqlclient
Downloading/unpacking mysqlclient
Downloading mysqlclient-1.3.6.tar.gz (78Kb): 78Kb downloaded
Running setup.py egg_info for package mysqlclient

Installing collected packages: mysqlclient
Running setup.py install for mysqlclient
building '_mysql' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,3,6,'final',1) -D__version__=1.3.6 -I/usr/include/mysql -I/usr/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o -DBIG_JOINS=1 -fno-strict-aliasing -g
In file included from /usr/include/python2.7/Python.h:8:0,
from _mysql.c:40:
/usr/include/python2.7/pyconfig.h:1161:0: attention : « _POSIX_C_SOURCE » redéfini [enabled by default]
/usr/include/features.h:164:0: note: ceci est la localisation d'une précédente définition
/usr/include/python2.7/pyconfig.h:1183:0: attention : « _XOPEN_SOURCE » redéfini [enabled by default]
/usr/include/features.h:166:0: note: ceci est la localisation d'une précédente définition
gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro build/temp.linux-x86_64-2.7/_mysql.o -L/usr/lib/x86_64-linux-gnu -lmysqlclient_r -lpthread -lz -lm -lrt -ldl -o build/lib.linux-x86_64-2.7/_mysql.so

Successfully installed mysqlclient
Cleaning up...
(venv)bruno@bigb:~/Work/playground$ python
Python 2.7.3 (default, Jun 22 2015, 19:33:41)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
pythonrc start
pythonrc done
>>> import MySQLdb
>>> >>> MySQLdb
<module 'MySQLdb' from '/home/bruno/Work/playground/venv/local/lib/python2.7/site-packages/MySQLdb/__init__.pyc'>

关于python - Python 虚拟环境中没有名为 MySqlDb 的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32715175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com