📜  python 2 和 python3 的 pip install - Python (1)

📅  最后修改于: 2023-12-03 15:18:53.278000             🧑  作者: Mango

Python2和Python3的pip install

Python是一种多版本编程语言,它有两个主要版本,即Python2和Python3。pip是Python包管理器,可以轻松安装和管理Python包。然而,Python2和Python3不完全兼容,这就导致了pip在这两个版本中的一些问题。在本文中,我们将探讨Python2和Python3之间pip install的差异,以及如何在两个版本中正确地使用pip。

Python2中pip install的问题

Python2在2010年被Python3取代。因此,它的pip包管理器不再得到维护。由于Python2和Python3不兼容,所以Python2的pip install不支持Python3的新特性。把Python3的包安装到Python2中,可能会导致一些问题。

下面是一个示例,演示了在Python2中安装Python3包的问题:

$ pip install requests
Collecting requests
  Using cached requests-2.25.1-py2.py3-none-any.whl (61 kB)
Collecting urllib3<1.27,>=1.21.1
  Using cached urllib3-1.26.6-py2.py3-none-any.whl (138 kB)
Collecting certifi>=2017.4.17
  Using cached certifi-2021.5.30-py2.py3-none-any.whl (145 kB)
Collecting chardet<5,>=3.0.2
  Using cached chardet-4.0.0-py2.py3-none-any.whl (178 kB)
Collecting idna<3,>=2.5
  Using cached idna-2.10-py2.py3-none-any.whl (58 kB)
Installing collected packages: urllib3, certifi, chardet, idna, requests
Successfully installed certifi-2021.5.30 chardet-4.0.0 idna-2.10 requests-2.25.1 urllib3-1.26.6

我们可以看到,在Python2中安装requests包时,pip通过使用缓存来安装Python3兼容包。结果,我们安装了Python3中的requests包,并且它可能无法在Python2中正常工作。

Python3中pip install的问题

Python3在2010年被发布。由于Python2和Python3不同,包括标准库的更改和语言语法的更新,因此Python2和Python3之间的兼容性不好。Python3的pip install与Python2的版本有一些不同。

下面是一个示例,展示了在Python3中安装包时可能会出现的问题:

$ pip install MySQL-python
Collecting MySQL-python
  Using cached MySQL-python-1.2.5.zip (108 kB)
Building wheels for collected packages: MySQL-python
  Building wheel for MySQL-python (setup.py) ... error
  ERROR: Command errored out with exit status 1:
  command: /usr/bin/python3 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-kdrgc_ae/MySQL-python_2633ae2aebd84073a810cb3997110565/setup.py'"'"'; __file__='"'"'/tmp/pip-install-kdrgc_ae/MySQL-python_2633ae2aebd84073a810cb3997110565/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r

'"'"', '"'"'

'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-d8rkzefh
  cwd: /tmp/pip-install-kdrgc_ae/MySQL-python_2633ae2aebd84073a810cb3997110565/
Complete output (30 lines):
Running setup.py bdist_wheel for MySQL-python: started
Running setup.py bdist_wheel for MySQL-python: finished with status 'error'
  ERROR: Command errored out with exit status 1:
   command: /usr/bin/python3 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-kdrgc_ae/MySQL-python_2633ae2aebd84073a810cb3997110565/setup.py'"'"'; __file__='"'"'/tmp/pip-install-kdrgc_ae/MySQL-python_2633ae2aebd84073a810cb3997110565/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r

'"'"', '"'"'

'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-og3vgjf7/install-record.txt --single-version-externally-managed --compile --user --prefix=
       cwd: /tmp/pip-install-kdrgc_ae/MySQL-python_2633ae2aebd84073a810cb3997110565/
  Complete output (29 lines):
  running install
  running build
  running build_py
  creating build/lib.linux-x86_64-3.8
  copying _mysql_exceptions.py -> build/lib.linux-x86_64-3.8
  creating build/lib.linux-x86_64-3.8/MySQLdb
  copying MySQLdb/__init__.py -> build/lib.linux-x86_64-3.8/MySQLdb
  copying MySQLdb/compat.py -> build/lib.linux-x86_64-3.8/MySQLdb
  copying MySQLdb/connections.py -> build/lib.linux-x86_64-3.8/MySQLdb
  copying MySQLdb/converters.py -> build/lib.linux-x86_64-3.8/MySQLdb
  copying MySQLdb/cursors.py -> build/lib.linux-x86_64-3.8/MySQLdb
  copying MySQLdb/release.py -> build/lib.linux-x86_64-3.8/MySQLdb
  copying MySQLdb/times.py -> build/lib.linux-x86_64-3.8/MySQLdb
  creating build/lib.linux-x86_64-3.8/MySQLdb/constants
  copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-3.8/MySQLdb/constants
  copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-3.8/MySQLdb/constants
  copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-3.8/MySQLdb/constants
  copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-3.8/MySQLdb/constants
  copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-3.8/MySQLdb/constants
  copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-3.8/MySQLdb/constants
  copying MySQLdb/constants/REFRESH.py -> build/lib.linux-x86_64-3.8/MySQLdb/constants
  copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-3.8/MySQLdb/constants
  running build_ext
  building '_mysql' extension
  creating build/temp.linux-x86_64-3.8
  creating build/temp.linux-x86_64-3.8/MySQLdb
  x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/include/mysql -I/usr/include/python3.8 -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-3.8/MySQLdb/_mysql.o
  MySQLdb/_mysql.c:46:10: fatal error: Python.h: No such file or directory
     46 | #include <Python.h>
        |          ^~~~~~~~~~
  compilation terminated.
  error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for MySQL-python
  Running setup.py clean for MySQL-python
Failed to build MySQL-python
Installing collected packages: MySQL-python
    Running setup.py install for MySQL-python: started
    Running setup.py install for MySQL-python: finished with status 'error'
    ERROR: Command errored out with exit status 1:
     command: /usr/bin/python3 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-kdrgc_ae/MySQL-python_2633ae2aebd84073a810cb3997110565/setup.py'"'"'; __file__='"'"'/tmp/pip-install-kdrgc_ae/MySQL-python_2633ae2aebd84073a810cb3997110565/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r

'"'"', '"'"'

'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-og3vgjf7/install-record.txt --single-version-externally-managed --compile --user --prefix=
         cwd: /tmp/pip-install-kdrgc_ae/MySQL-python_2633ae2aebd84073a810cb3997110565/
    Complete output (29 lines):
    running install
    running build
    running build_py
    creating build/lib.linux-x86_64-3.8
    copying _mysql_exceptions.py -> build/lib.linux-x86_64-3.8
    creating build/lib.linux-x86_64-3.8/MySQLdb
    copying MySQLdb/__init__.py -> build/lib.linux-x86_64-3.8/MySQLdb
    copying MySQLdb/compat.py -> build/lib.linux-x86_64-3.8/MySQLdb
    copying MySQLdb/connections.py -> build/lib.linux-x86_64-3.8/MySQLdb
    copying MySQLdb/converters.py -> build/lib.linux-x86_64-3.8/MySQLdb
    copying MySQLdb/cursors.py -> build/lib.linux-x86_64-3.8/MySQLdb
    copying MySQLdb/release.py -> build/lib.linux-x86_64-3.8/MySQLdb
    copying MySQLdb/times.py -> build/lib.linux-x86_64-3.8/MySQLdb
    creating build/lib.linux-x86_64-3.8/MySQLdb/constants
    copying MySQLdb/constants/__init__.py -> build/lib.linux-x86_64-3.8/MySQLdb/constants
    copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-3.8/MySQLdb/constants
    copying MySQLdb/constants/CR.py -> build/lib.linux-x86_64-3.8/MySQLdb/constants
    copying MySQLdb/constants/ER.py -> build/lib.linux-x86_64-3.8/MySQLdb/constants
    copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.linux-x86_64-3.8/MySQLdb/constants
    copying MySQLdb/constants/FLAG.py -> build/lib.linux-x86_64-3.8/MySQLdb/constants
    copying MySQLdb/constants/REFRESH.py -> build/lib.linux-x86_64-3.8/MySQLdb/constants
    copying MySQLdb/constants/CLIENT.py -> build/lib.linux-x86_64-3.8/MySQLdb/constants
    running build_ext
    building '_mysql' extension
    creating build/temp.linux-x86_64-3.8
    creating build/temp.linux-x86_64-3.8/MySQLdb
    x86_64-linux-gnu-gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -Dversion_info=(1,2,5,'final',1) -D__version__=1.2.5 -I/usr/include/mysql -I/usr/include/python3.8 -c MySQLdb/_mysql.c -o build/temp.linux-x86_64-3.8/MySQLdb/_mysql.o
    MySQLdb/_mysql.c:46:10: fatal error: Python.h: No such file or directory
       46 | #include <Python.h>
          |          ^~~~~~~~~~
    compilation terminated.
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /usr/bin/python3 -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-kdrgc_ae/MySQL-python_2633ae2aebd84073a810cb3997110565/setup.py'"'"'; __file__='"'"'/tmp/pip-install-kdrgc_ae/MySQL-python_2633ae2aebd84073a810cb3997110565/setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'\r

'"'"', '"'"'

'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-og3vgjf7/install-record.txt --single-version-externally-managed --compile --user --prefix= Check the logs for full command output.

我们可以看到,当我们在Python3中尝试安装MySQL-python时,编译错误发生了。这是因为此软件包依赖于Python.h文件,它可能不存在于我们的操作系统上。我们可以通过安装操作系统本身的“python3-devel”软件包来解决此问题。这种情况并不经常发生,但我们需要高度注意,可能需要一些更改才能在Python3中使用pip。

两个版本中正确使用pip install

为了正确地使用pip install,我们建议遵循以下最佳实践:

  1. 尽可能使用Python3和Python3包。如果必须使用Python2,请确保在Python2中安装正确版本的软件包。使用pip的“--version”选项指定版本。
  2. 在使用pip时,使用“pip3”而不是“pip”。这将确保在Python3中安装包,并避免可能的版本冲突。
  3. 如果必须跨Python版本安装Python包,请避免在同一目录结构中安装相同的包,并避免使用“--user”选项。这将确保在每个Python版本的不同目录中安装软件包,并避免可能的混淆。

为了更好地了解如何使用pip install,请参阅以下资源:

  • 官方pip文档:https://pip.pypa.io/en/stable/
  • Python2和Python3之间的差异:https://docs.python.org/3/whatsnew/3.0.html#porting-to-python-3-0
  • Python2和Python3之间的语法差异:https://sebastianraschka.com/Articles/2014_python_2_3_key_diff.html
结论

在Python2和Python3中,使用pip安装Python包时,可能会遇到问题。遵循最佳实践,并时刻牢记两个Python版本之间的差异,可以提高使用pip的成功率。 您可以通过阅读本文中提到的资源来深入了解如何正确使用pip install。