UOS更换python3.7及安装pip与pyinstaller及更换pip源提高下载速率

1. 安装所需的Python版本

首先,确保您安装了所需的Python版本。UOS的软件仓库可能直接提供Python 3.5或更高版本。您可以使用包管理器进行安装,例如:

sudo apt update
sudo apt install python3.7  # 举例使用Python 3.7,根据需要选择合适版本

2. 更新Python的默认版本

安装好所需的Python版本后,需要设置系统默认的Python版本。这可以通过更新系统的符号链接来实现:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 1

此命令将python3.7(或您安装的任何版本)设置为python命令的默认版本。如果系统中已有其他版本的Python作为默认设置,可能还需要通过update-alternatives配置来选择默认版本:

sudo update-alternatives --config python

如果您遇到错误“sudo: update-alternatives: 找不到命令”,这意味着update-alternatives工具可能没有安装在您的系统上,或者其安装路径没有被包含在环境变量PATH中。update-alternatives是Debian及其衍生系统(如Ubuntu)中的一个工具,用于管理系统中安装的多个软件版本。对于基于Debian的UOS系统,update-alternatives应该预装在系统中。如果确实遇到了找不到命令的情况,您可以尝试以下几个步骤来解决:

2.1. 确认update-alternatives是否安装

尝试直接运行update-alternatives看是否能够执行。如果系统提示找不到命令,那可能确实是未安装或路径问题。

2.2. 检查update-alternatives的安装路径

通常,update-alternatives位于/usr/bin/update-alternatives。您可以通过以下命令确认:

ls /usr/bin/update-alternatives

如果该命令显示了文件路径,则表示update-alternatives已经安装,可能是PATH环境变量的问题。确保/usr/bin在您的PATH中。

2.3. 确保/usr/bin在您的PATH环境变量中

运行echo $PATH查看当前的PATH环境变量,确认其中包含/usr/bin。如果没有,您可以通过运行以下命令临时添加它:

export PATH=$PATH:/usr/bin

这将仅对当前终端会话有效。要永久更改,您需要将上述命令添加到您的shell配置文件中,如.bashrc.profile

2.4. 如果确实没有安装

如果通过上述步骤确认update-alternatives确实没有安装,您可能需要通过包管理器手动安装。由于UOS基于Debian,您可以尝试使用apt安装:

sudo apt update
sudo apt install dpkg

update-alternativesdpkg的一部分,因此安装或重新安装dpkg包应该会提供update-alternatives工具。

2.5. 使用其他方法更改默认Python版本

如果出于某种原因,您无法通过update-alternatives成功更改默认的Python版本,您可以考虑编辑~/.bashrc~/.bash_aliases文件,为python命令添加别名指向新的Python版本:

echo "alias python='/usr/bin/python3.7'" >> ~/.bashrc
source ~/.bashrc

3. 安装与Python版本相匹配的pip

Python 3.5及以上版本通常会自带pip。如果您通过上述步骤安装了Python,pip也应该已经安装了。您可以通过运行pip3 --version来检查是否安装以及当前版本。

如果需要更新pip或确保其与新安装的Python版本匹配,可以使用以下命令:

python -m ensurepip
python -m pip install --upgrade pip

这将确保pip已经安装,并且是最新版本

如果您执行python -m ensurepip时遇到“No module named ensurepip”的错误,这表明您当前使用的Python版本中没有包含ensurepip模块。ensurepip模块通常用于安装或升级pip,它自Python 3.4版本起就被包含在Python标准库中。如果您遇到这个问题,可能有以下几个原因:

  1. 使用的Python版本过旧:如果您使用的是Python 3.4以下的版本,ensurepip是不可用的。您提到需要Python 3.5以上,因此请验证您的Python版本确实满足此要求。您可以通过运行python --version来检查当前Python的版本。

  2. Python安装不完整:在某些情况下,如果Python是通过某些最小化安装程序安装的,可能不包含所有标准库模块,包括ensurepip。这种情况下,您可能需要重新安装Python,确保包含了完整的标准库。

为了解决这个问题,您有几个选项:

解决方案 1:直接安装pip

如果您的系统中的Python版本是3.5或更高,但缺少ensurepip,您可以尝试直接安装pip。对于许多Linux发行版,pip可以通过包管理器直接安装。对于基于Debian的系统,如UOS,您可以使用:

sudo apt update
sudo apt install python3-pip

解决方案 2:使用其他Python版本

如果您系统中安装了多个Python版本,确保您正在使用的是正确的版本。您可以通过指定版本号来运行Python,例如python3.7,然后尝试ensurepip

python3.7 -m ensurepip

替换3.7为您系统中实际安装的Python版本。

解决方案 3:重新安装Python

如果您确定系统中的Python版本应该包含ensurepip,但出于某种原因未包含,可能需要重新安装Python。这可以通过您的包管理器完成,或者从Python官网下载相应版本的安装程序进行安装。

在重新安装Python时,确保从官方渠道或通过系统的包管理器安装,以获取完整的标准库和功能。

解决方案 4:手动下载和安装pip

如果以上方法都不适用,您可以按照pip官方文档中推荐的方法,通过下载get-pip.py脚本来安装pip

  1. 下载get-pip.py脚本(由于您可能无法直接从当前环境访问互联网,需要在可以访问互联网的设备上下载,然后传输到当前系统)。
  2. 在命令行中运行该脚本:
    python get-pip.py
    

    如果您已经通过sudo apt install python3-pip安装了pip,但是系统仍然找不到pip命令,这可能是由于以下几个原因造成的:

  3. 1. 环境变量问题

    在某些情况下,pip安装成功后,可能由于环境变量没有正确设置,导致终端找不到pip命令。通常,pip的执行文件位于/usr/bin/pip3/usr/local/bin/pip3

    您可以通过直接调用pip3来测试是否能运行pip:

    pip3 --version
    

    如果这个命令成功执行,那么pip实际上是已经安装的,只是通过pip命令无法访问。在这种情况下,您应该始终使用pip3命令来代替pip,以确保您在使用Python 3的pip。

    2. 创建别名

    如果您习惯于使用pip命令而不是pip3,可以在您的shell配置文件中(如.bashrc.zshrc)创建一个别名:

    echo "alias pip=pip3" >> ~/.bashrc
    source ~/.bashrc
    

    这样,当您在终端中输入pip时,实际上会运行pip3

    3. 重新加载环境变量

    如果您认为环境变量的问题导致pip3无法被识别,尝试重新加载环境变量或重新开启一个终端会话,然后再次检查。

    source ~/.bashrc
    

    或者简单地重新开启一个终端窗口。

    4. 确认pip安装路径

    如果以上步骤仍然不能解决问题,尝试查找pip的安装路径。这可以通过以下命令完成:

    whereis pip3
    

    这将显示pip3的安装路径。如果pip3被安装在非标准路径或者不在您的PATH环境变量中,您可能需要手动将其添加到PATH中。

    5. 检查是否为多个Python版本造成的混乱

    如果您的系统中安装了多个Python版本,确保您使用的是系统认为的"默认"Python版本的pip。有时候,不同的Python版本可能会导致路径和版本管理上的混乱。

    6. 使用python3 -m pip

    最后,即使pippip3命令不直接工作,您仍然可以通过Python自身来运行pip模块:

    python3 -m pip --version
    

3.uos安装pyqt5 

在UOS(统信操作系统,一个基于Debian的Linux发行版)上安装PyQt5,您可以通过使用系统的包管理器apt来完成。PyQt5是一套用于创建图形界面应用程序的Python绑定,它为Qt5框架提供了Python API。请按照以下步骤操作:

  1. 打开终端:首先,打开您的终端。

  2. 更新包列表:运行以下命令来更新您的系统包列表。这确保您安装的是最新版本的软件包

    sudo apt update
    

 *重点,安装前先查看PyQt5版本不然有可能版本不对,导致执行文件执行报错:

检查 PyQt5 版本: 你可以使用 Python 的交互模式来快速检查 PyQt5 的版本。输入以下命令:

python3 -c "from PyQt5.QtCore import QT_VERSION_STR; print(QT_VERSION_STR)"

或者,如果你想知道 PyQt5 的更多详细信息(例如 SIP 的版本),可以使用以下命令:

python3 -c "from PyQt5.QtCore import QT_VERSION_STR, PYQT_VERSION_STR; import sip; print('Qt version:', QT_VERSION_STR, '\nPyQt version:', PYQT_VERSION_STR, '\nSIP version:', sip.SIP_VERSION_STR)"

 

 3.安装PyQt5:使用apt安装PyQt5。在UOS上,PyQt5可以直接通过包管理器安装。

sudo apt install python3-pyqt5

这个命令将安装PyQt5及其所有依赖项。安装完成后,您就可以开始在Python中使用PyQt5了。

验证安装

安装完成后,您可以通过运行一个简单的Python脚本来验证PyQt5是否正确安装:

  1. 打开文本编辑器,创建一个新的Python文件,例如test_pyqt5.py

  2. 复制并粘贴以下代码到文件中:

    import sys
    from PyQt5.QtWidgets import QApplication, QWidget
    
    def main():
        app = QApplication(sys.argv)
        w = QWidget()
        w.resize(250, 150)
        w.move(300, 300)
        w.setWindowTitle('Simple')
        w.show()
        
        sys.exit(app.exec_())
    
    if __name__ == '__main__':
        main()
    

  3. 保存文件并关闭编辑器。

  4. 在终端中,导航到文件所在的目录,运行脚本:

    python3 test_pyqt5.py
    

4. 安装pyinstalle

在UOS上安装PyInstaller,您可以通过Python的包管理工具pip来完成。PyInstaller是一个流行的工具,它可以将Python应用程序打包成独立的可执行文件,使其可以在没有安装Python解释器的系统上运行。下面是安装PyInstaller的步骤:

有了pip之后,您可以直接安装PyInstaller。在终端中运行以下命令:

pip3 install pyinstaller

如果安装过程下载比较慢,可以采用下面方式换源

清华大学镜像源

pip3 install pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple

中国科技大学镜像源

pip3 install pyinstaller -i https://pypi.mirrors.ustc.edu.cn/simple

阿里云镜像源

pip3 install pyinstaller -i https://mirrors.aliyun.com/pypi/simple/

豆瓣镜像源

pip3 install pyinstaller -i https://pypi.douban.com/simple

使用这些命令中的任何一个都会从指定的镜像源安装PyInstaller,通常能够提供比默认源更快的下载速度。

永久更改pip源(可选)

如果您经常需要从这些镜像源安装包,您可能希望将pip配置为默认使用其中一个镜像源。要这样做,可以创建或修改~/.pip/pip.conf(Linux或macOS)或%USERPROFILE%\pip\pip.ini(Windows)文件,添加以下内容:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

请将index-url的值替换为您选择的镜像源地址。这样,未来使用pip安装任何包时,都会默认使用这个镜像源,而不需要每次指定-i选项。

如果没有找到~/.pip/pip.conf这个目录

1. 打开终端

首先,在您的系统上打开一个终端窗口。

2. 检查目录是否存在

在终端中,输入以下命令来检查.pip目录是否已经存在于您的主目录中:

ls ~/.pip

如果终端显示错误消息,如“No such file or directory”,则表示该目录尚未创建。

3. 创建目录和配置文件(如有必要)

如果.pip目录不存在,您可以使用以下命令创建它及其配置文件:

mkdir -p ~/.pip
touch ~/.pip/pip.conf

这些命令分别创建了.pip目录(如果它还不存在的话)和一个空的pip.conf文件。

4. 编辑pip.conf文件

接下来,您需要编辑这个新创建的pip.conf文件,以添加您选择的镜像源。您可以使用任何文本编辑器

nano ~/.pip/pip.conf

来编辑这个文件,例如使用nano

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

完成后,保存并关闭文件。在nano中,您可以通过按下Ctrl + O保存更改,然后按Ctrl + X退出编辑器。

5. 验证配置

为了验证pip现在是否默认使用您指定的镜像源,您可以运行一个如pip install的命令,但不实际安装任何包。例如:

pip3 install --verbose --dry-run somepackage

这将显示pip尝试连接到的URL,从中您可以确认是否正在使用新的镜像源。注意,--dry-run选项在较新版本的pip中可用,用于模拟安装过程而不实际安装包。

按照上述步骤,您应该能够成功设置pip的默认镜像源,以便在安装Python包时获得更快的下载速度。

执行pip3 install pyinstaller 报如下错误:

oot@user-PC:/home/user/Desktop# pip3 install pyinstaller
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pyinstaller
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/bd/23/c5f0163b2049699cdbb511eac72798f017d4c9a3f4ba571fbef398156e3d/pyinstaller-5.13.2.tar.gz (4.1MB)
    100% |████████████████████████████████| 4.1MB 197kB/s 
  Installing build dependencies ... done
Collecting setuptools>=42.0.0 (from pyinstaller)
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/c7/42/be1c7bbdd83e1bfb160c94b9cafd8e25efc7400346cf7ccdbdb452c467fa/setuptools-68.0.0-py3-none-any.whl
Collecting altgraph (from pyinstaller)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/4d/3f/3bc3f1d83f6e4a7fcb834d3720544ca597590425be5ba9db032b2bf322a2/altgraph-0.17.4-py2.py3-none-any.whl
Collecting pyinstaller-hooks-contrib>=2021.4 (from pyinstaller)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/d4/bc/7e593b05aecd3dcaa5a96ba567662ebae2384a27ae30d5aa03973249f19e/pyinstaller_hooks_contrib-2024.3-py2.py3-none-any.whl (329kB)
    100% |████████████████████████████████| 337kB 2.4MB/s 
Collecting importlib-metadata>=1.4 (from pyinstaller)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ff/94/64287b38c7de4c90683630338cf28f129decbba0a44f0c6db35a873c73c4/importlib_metadata-6.7.0-py3-none-any.whl
Collecting packaging>=22.0 (from pyinstaller-hooks-contrib>=2021.4->pyinstaller)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/49/df/1fceb2f8900f8639e278b056416d49134fb8d84c5942ffaa01ad34782422/packaging-24.0-py3-none-any.whl (53kB)
    100% |████████████████████████████████| 61kB 6.9MB/s 
Collecting zipp>=0.5 (from importlib-metadata>=1.4->pyinstaller)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/5b/fa/c9e82bbe1af6266adf08afb563905eb87cab83fde00a0a08963510621047/zipp-3.15.0-py3-none-any.whl
Collecting typing-extensions>=3.6.4; python_version < "3.8" (from importlib-metadata>=1.4->pyinstaller)
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/ec/6b/63cc3df74987c36fe26157ee12e09e8f9db4de771e0f3404263117e75b95/typing_extensions-4.7.1-py3-none-any.whl
Building wheels for collected packages: pyinstaller
  Running setup.py bdist_wheel for pyinstaller ... error
  Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-w0suw4y_/pyinstaller/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/pip-wheel-oofnyfbw --python-tag cp37:
  /tmp/pip-build-env-vxgza07p/lib/python3.7/site-packages/setuptools/__init__.py:84: _DeprecatedInstaller: setuptools.installer and fetch_build_eggs are deprecated.
  !!
  
          ********************************************************************************
          Requirements should be satisfied by a PEP 517 installer.
          If you are using pip, you can try `pip install --use-pep517`.
          ********************************************************************************
  
  !!
    dist.fetch_build_eggs(dist.setup_requires)
  running bdist_wheel
  running build
  running build_bootloader
  No precompiled bootloader found or compile forced. Trying to compile the bootloader for you ...
  Setting top to                           : /tmp/pip-install-w0suw4y_/pyinstaller/bootloader
  Setting out to                           : /tmp/pip-install-w0suw4y_/pyinstaller/bootloader/build
  Python Version                           : 3.7.3 (default, Oct 13 2023, 10:47:37) [GCC 8.3.0]
  Checking for 'gcc' (C compiler)          : /usr/bin/gcc
  Checking size of pointer                 : 8
  Platform                                 : Linux-64bit-intel detected based on compiler
  Checking for compiler flags -m64         : yes
  Checking for linker flags -m64           : yes
  Checking for compiler flags -Wno-error=unused-but-set-variable : yes
  Checking for library dl                                        : yes
  Checking for library pthread                                   : yes
  Checking for library m                                         : yes
  Checking for library z                                         : no
  The zlib development package is either missing or the shared library cannot be linked against. For security (and marginally better filesize), you should install the zlib-dev or zlib-devel packages with your system package manager, and try again. If you cannot do this (for example, distributions such as OpenWRT use sstrip on libraries, making linking impossible), then either use the --static-zlib option or set the PYI_STATIC_ZLIB=1 environment variable. If you are installing directly with pip, then use the environment variable.
  (complete log in /tmp/pip-install-w0suw4y_/pyinstaller/bootloader/build/config.log)
  ERROR: Failed compiling the bootloader. Please compile manually and rerun setup.py
  
  ----------------------------------------
  Failed building wheel for pyinstaller
  Running setup.py clean for pyinstaller
Failed to build pyinstaller
Installing collected packages: setuptools, altgraph, packaging, zipp, typing-extensions, importlib-metadata, pyinstaller-hooks-contrib, pyinstaller
  Found existing installation: setuptools 40.8.0
    Not uninstalling setuptools at /usr/lib/python3/dist-packages, outside environment /usr
    Can't uninstall 'setuptools'. No files were found to uninstall.
  Running setup.py install for pyinstaller ... error
    Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-w0suw4y_/pyinstaller/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-7iwkmwsh/install-record.txt --single-version-externally-managed --compile:
    /tmp/pip-build-env-vxgza07p/lib/python3.7/site-packages/setuptools/__init__.py:84: _DeprecatedInstaller: setuptools.installer and fetch_build_eggs are deprecated.
    !!
    
            ********************************************************************************
            Requirements should be satisfied by a PEP 517 installer.
            If you are using pip, you can try `pip install --use-pep517`.
            ********************************************************************************
    
    !!
      dist.fetch_build_eggs(dist.setup_requires)
    running install
    /tmp/pip-build-env-vxgza07p/lib/python3.7/site-packages/setuptools/_distutils/cmd.py:66: SetuptoolsDeprecationWarning: setup.py install is deprecated.
    !!
    
            ********************************************************************************
            Please avoid running ``setup.py`` directly.
            Instead, use pypa/build, pypa/installer or other
            standards-based tools.
    
            See https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html for details.
            ********************************************************************************
    
    !!
      self.initialize_options()
    running build
    running build_bootloader
    No precompiled bootloader found or compile forced. Trying to compile the bootloader for you ...
    Setting top to                           : /tmp/pip-install-w0suw4y_/pyinstaller/bootloader
    Setting out to                           : /tmp/pip-install-w0suw4y_/pyinstaller/bootloader/build
    Python Version                           : 3.7.3 (default, Oct 13 2023, 10:47:37) [GCC 8.3.0]
    Checking for 'gcc' (C compiler)          : /usr/bin/gcc
    Checking size of pointer                 : 8
    Platform                                 : Linux-64bit-intel detected based on compiler
    Checking for compiler flags -m64         : yes
    Checking for linker flags -m64           : yes
    Checking for compiler flags -Wno-error=unused-but-set-variable : yes
    Checking for library dl                                        : yes
    Checking for library pthread                                   : yes
    Checking for library m                                         : yes
    Checking for library z                                         : no
    The zlib development package is either missing or the shared library cannot be linked against. For security (and marginally better filesize), you should install the zlib-dev or zlib-devel packages with your system package manager, and try again. If you cannot do this (for example, distributions such as OpenWRT use sstrip on libraries, making linking impossible), then either use the --static-zlib option or set the PYI_STATIC_ZLIB=1 environment variable. If you are installing directly with pip, then use the environment variable.
    (complete log in /tmp/pip-install-w0suw4y_/pyinstaller/bootloader/build/config.log)
    ERROR: Failed compiling the bootloader. Please compile manually and rerun setup.py
    
    ----------------------------------------
Command "/usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-w0suw4y_/pyinstaller/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-7iwkmwsh/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-w0suw4y_/pyinstaller/    
root@user-PC:/home/user/Desktop# 

根据您提供的错误信息,安装PyInstaller时遇到的主要问题是编译其bootloader时缺少zlib开发包。这是PyInstaller编译过程中需要的一个依赖项。错误消息建议安装zlib的开发包版本,并再次尝试安装。这里是如何解决这个问题的步骤:

安装zlib开发包

在Debian或基于Debian的系统(如UOS)上,您可以通过运行以下命令来安装zlib的开发版本:

sudo apt update
sudo apt install zlib1g-dev

这将安装zlib库的开发文件,允许PyInstaller的bootloader编译过程成功完成。

再次尝试安装PyInstaller

在成功安装zlib1g-dev包之后,再次尝试安装PyInstaller:

pip3 install pyinstaller

如果仍然遇到问题

如果在安装zlib1g-dev并尝试重新安装PyInstaller之后仍然遇到问题,这里有一些额外的建议:

  • 确保系统和所有软件包都是最新的:有时,依赖关系或兼容性问题可以通过更新系统和所有已安装软件包来解决。

  • sudo apt update
    sudo apt upgrade
    

 尝试使用PEP 517构建系统:错误消息中提到,您可以尝试使用--use-pep517选项。这是一个较新的Python打包标准,可能有助于解决安装问题。

pip3 install --use-pep517 pyinstaller

检查gcc和其他构建工具是否已安装:编译PyInstaller的bootloader需要gcc和其他构建工具。确保这些工具已正确安装:

sudo apt install build-essential

  • 23
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值