Pymysql 无法导入 python3

Pymysql failing to import into python3

我正在使用 LAMP 在 debian 9 机器上从 GCP 安装。 Default GCP Lamp settings https://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 服务器的新实例上为我修复了它。我不能(放弃)去研究另一个。我假设我安装或卸载的东西像 snakecharmerb 所建议的那样搞砸了,但是当我可以制作一个新服务器并且只做 apt install python3-[ 时,我不值得花时间去寻找它。 =39=]从头开始。

谢谢。

编辑编辑 这是我在新服务器上遵循的过程,似乎对我来说设置正确

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

On my Debian 9 machine, sudo apt-get install pymysql installs PyMySQL (0.7.10) (according to pip3 list), which imports without error in the interpreter. It looks like you have installed a later version locally, which contains syntax that isn't compatible with Python 3.5. I'd suggest uninstalling the version in ~/.local and trying to use the version installed by apt

snakecharmerb 提供了使用 sudo apt install python3-pymysql 的建议。这在 GCP Lamp 服务器的新实例上为我修复了它。我不能(放弃)去研究另一个。我假设这对我来说是愚蠢的——我安装或卸载的东西搞砸了,正如 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