【问题标题】:Pymysql failing to import into python3Pymysql无法导入python3
【发布时间】:2021-05-31 02:12:55
【问题描述】:

我正在 debian 9 机器上从 GCP 安装 LAMP。 Default GCP Lamp settingshttps://cloud.google.com/solutions/web-hosting

我在尝试将 pymysql 导入项目时遇到一些依赖错误。我不完全理解错误,或者确切地说我错过了什么。这是我得到的错误:

    >>> import pymysql
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/philip_plachta/.local/lib/python3.5/site-packages/pymysql/__init__.py", line 59, in <module>
    from . import connections  # noqa: E402
  File "/home/philip_plachta/.local/lib/python3.5/site-packages/pymysql/connections.py", line 206
    ):

这是我目前安装的python包

~$ python3 -m pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format= 
(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
certifi (2020.12.5)
chardet (4.0.0)
connect.py (0.4.2)
cryptography (1.7.1)
idna (2.10)
keyring (10.1)
keyrings.alt (1.3)
mysql-connector-python (8.0.23)
pip (9.0.1)
protobuf (3.15.3)
pyasn1 (0.1.9)
pycrypto (2.6.1)
pycurl (7.43.0)
pygobject (3.22.0)
PyMySQL (1.0.0)
python-apt (1.4.3)
pyxdg (0.25)
requests (2.25.1)
SecretStorage (2.3.1)
setuptools (33.1.1)
six (1.10.0)
unattended-upgrades (0.1)
urllib3 (1.26.3)
wheel (0.29.0)

Python3 import Error

以下是我尝试过的大致内容。它变得有点混乱,因为我尝试了很多东西并卸载了东西并重新安装了它们等等。我不反对从头开始,但如果我能从比我更有经验的人那里得到一点指导,我将不胜感激!

sudo apt-get install python3-pip
sudo apt-get install python3-pymysql    
sudo python3 -m pip install Pymysql
sudo python3 -m pip install mysql-connector-python

编辑/解决方案

snakecharmerb 提供了使用 sudo apt install python3-pymysql 的建议。这为我在 GCP Lamp Server 的新实例上修复了它。我无法(放弃)从事另一项工作。我假设我安装或卸载的东西像snakecharmerb所暗示的那样搞砸了,但是当我可以制作一个新服务器并从一开始就执行apt install python3-mysql时,我不值得花时间去尝试找到它。

谢谢。

编辑编辑 这是我在新服务器上遵循的过程,似乎为我正确设置了东西

FROM THE START:
(First Goal is to access mysql from my local machine)
    sudo apt update
    sudo apt install php7.4-mbstring -y
    
(Create a mysql user)
    sudo mysql -u root -p
    CREATE USER 'username'@'%.%.%.%' IDENTIFIED BY 'password';
    GRANT ALL PRIVILEGES ON *. * TO 'username'@'%.%.%.%';
    
(Open to the outside world)
    https://phoenixnap.com/kb/mysql-remote-connection
    Also had to create an ingress rule to allow sql to be accessed (via firewall in gcp) on 0.0.0.0/0 on port 3306

(Then...)
    sudo apt update
    sudo apt list --upgradable
    sudo apt upgrade
    
(Second goal is to allow my scripts to run on the machine)
    sudo apt install python3-pip -y
    sudo apt install python3-pymysql -y
    sudo apt install python3-mysql.connector -y

【问题讨论】:

  • 在我的 Debian 9 机器上,sudo apt-get install pymysql 安装 PyMySQL (0.7.10)(根据 pip3 list),它在解释器中导入时不会出错。您似乎在本地安装了更高版本,其中包含与 Python 3.5 不兼容的语法。我建议卸载~/.local中的版本并尝试使用apt安装的版本。
  • 非常感谢。这解决了我在新机器上的问题现在我将尝试在原始机器上查看是否可以将其恢复到正常位置。 'sudo apt install python3-pymysql' 现在允许我导入该库。 snakecharmerb 我很感激。

标签: python debian python-3.5 pymysql


【解决方案1】:

在我的 Debian 9 机器上,sudo apt-get install pymysql 安装 PyMySQL (0.7.10)(根据 pip3 列表),它在 口译员。看起来你在本地安装了更高版本, 其中包含与 Python 3.5 不兼容的语法。 ID 建议卸载 ~/.local 中的版本并尝试使用 apt安装的版本

snakecharmerb 提供了使用 sudo apt install python3-pymysql 的建议。这为我在 GCP Lamp Server 的新实例上修复了它。我无法(放弃)从事另一项工作。我假设这对我来说是一件愚蠢的事情——我安装或卸载的东西把事情搞砸了,就像蛇怪暗示的那样,但是当我可以建立一个新服务器并做 'apt 时,我不值得花时间去尝试找到它从一开始就安装python3-mysql'。

谢谢。

编辑/我在新服务器上运行的过程

FROM THE START:
(First Goal is to access mysql from my local machine)
    sudo apt update
    sudo apt install php7.4-mbstring -y
    
(Create a mysql user)
    sudo mysql -u root -p
    CREATE USER 'username'@'%.%.%.%' IDENTIFIED BY 'password';
    GRANT ALL PRIVILEGES ON *. * TO 'username'@'%.%.%.%';
    
(Open to the outside world)
    https://phoenixnap.com/kb/mysql-remote-connection
    Also had to create an ingress rule to allow sql to be accessed (via firewall in gcp) on 0.0.0.0/0 on port 3306

(Then...)
    sudo apt update
    sudo apt list --upgradable
    sudo apt upgrade
    
(Second goal is to allow my scripts to run on the machine)
    sudo apt install python3-pip -y
    sudo apt install python3-pymysql -y
    sudo apt install python3-mysql.connector -y

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-17
    • 2020-07-17
    • 2018-11-06
    • 2017-01-21
    • 2018-09-19
    • 2022-01-03
    • 2017-11-26
    • 2017-02-15
    相关资源
    最近更新 更多