Pycharm中包的导出和导入,以及超时解决,用源码或wheel安装

感谢丰哥,么么哒.

python解释器的包存放位置:安装目录\Lib\site-packages中,

pycharm包存放位置:pycharm的工程文件\venv\Lib\site-packages,比如:D:\PyCharm 2018.2.4\test\venv\Lib\site-packages  这里test为工程目录文件

如果要省事,可以把pycharm工程文件中的pyvenv.cfg中的include-system-site-packages 设置为 true   比如:D:\PyCharm 2018.2.4\test\venv\pyvenv.cfg

也是刚了解到PYCHARM中是有虚拟环境这个东西,所以每个工程之间的包都是不公用的.也是为了项目稳定考虑.

-------------------------------------------------分割线--------------------------------------------

导出pycharm的包名 terminal 中cd到工程文件的目录中  pip freeze > abc.txt 

abc.txt为保存在当前目录中的文件名

导入pycharm包名

工程文件目录中 pip install -r abc.txt.

注意一点的是由于如果导入的时候工程文件没包,会自行安装,由于pip安装默认的访问地址为 http://pypi.python.org/simple/经常会有网络不稳定和速度慢的现象,出现timeout报错,因此可以改为访问国内的地址加速下载。

可以在先前命令中   加上  -i https://mirrors.aliyun.com/pypi/simple/ 避免超时安装失败的情况.

完整命令为:pip install -r dd.txt -i https://mirrors.aliyun.com/pypi/simple/

国内常用的镜像源有 :
阿里云 http://mirrors.aliyun.com/pypi/simple/ 
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/ 
豆瓣(douban) http://pypi.douban.com/simple/ 
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/ 
华中理工大学 : http://pypi.hustunique.com/simple

山东理工大学 : http://pypi.sdutlinux.org/simple

或者可以修改超时时间限制

pip --default-timeout=100 install -U somepackage(somepackage是你要安装的包)

-------------------------------------------------分割线--------------------------------------------

更改pip的源地址

临时更改

见上文

永久更改

在C:\Users\用户名\AppData\Roaming\  路径中新建文件夹pip,在里面放pip.ini

pip.ini中复制以下内容

[global]

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

timeout = 6000

[install]
trusted-host=pypi.tuna.tsinghua.edu.cn

disable-pip-version-check = true

 -------------------------------------------------分割线--------------------------------------------

用wheel安装,whl相当于python模块的压缩包,可以进行离线安装.

进入到该wheel的目录,然后使用 pip install xxx.whl 命令就能离线安装

默认都是安装到以下默认文件夹

 

用源码安装,如果下载二进制源码包安装,要利用源码包里的setup.py

python setup.py build   --进行编译

python setup.py install --进行安装

 

wheel包和tar包

tar 包(通常是 .tar.gz 格式):这些包含了项目的源代码,包括 setup.py 文件和其他可能需要构建和安装包的文件。源代码包通常需要在安装时编译,这可能需要特定的依赖和工具。这种格式的包对于所有平台都是通用的,但安装过程可能会因为编译而变得更长,也有可能因为缺少编译依赖而失败。

wheel 包(.whl 格式):wheel 是一种预编译的、为快速安装设计的包格式。它不需要编译步骤,因此安装速度更快,也避免了编译上可能遇到的问题。wheel 文件通常是特定于平台的,这意味着它们可能只能在与构建它们的系统相同或兼容的系统上工作。

如何只下载某个类型的包

只下载 wheel 包:使用 --only-binary=:all: 选项,这将使 pip 只下载 wheel 格式的包。

pip download <package_name> --only-binary=:all:
只下载 tar 包:使用 --no-binary=:all: 选项,这将使 pip 只下载源码格式的包。

pip download <package_name> --no-binary=:all:

使用--prefer-binary选项,pip将优先下载wheel格式的包,但如果没有可用的wheel,则会退回到源码包。
pip download <package_name> --prefer-binary
请注意,如果特定包的wheel格式不可用,即便使用了上述选项,你也可能无法下载到wheel格式的包。这可能是因为该包没有预编译的wheel,或者预编译的wheel不支持你的平台。

在某些情况下,如果你知道要下载的wheel与另一个平台兼容,你可以使用--platform、--python-version、--implementation和--abi参数指定目标平台的兼容性:

--implementation
cp - CPython:默认的、最常见的Python实现。
pp - PyPy:一个使用JIT编译器的Python实现,旨在提高执行速度。
ip - IronPython:主要针对.NET平台的Python实现。
jy - Jython:运行在Java平台上的Python实现。

--abi 参数可以确保下载的 wheel 包符合预期的ABI,这样当你在一个特定的环境中安装包时可以保证兼容性。这对于跨平台部署或在有特定需求的系统上准备包非常有用。

常见的 ABI 标签包括:
cp36m - CPython 3.6 ABI,包括了PEP 384所定义的限制,该ABI标签通常包含了C语言编译器的mangle标签(在Linux上是m)。
cp36dm - 与 cp36m 类似,但包含了调试符号(d表示调试)。
cp37-cp37m - CPython 3.7 ABI。
abi3 - 用于遵循PEP 384所定义的Python稳定ABI的轮子文件。
none - 表示不依赖于特定ABI的包,这对于纯Python包是典型的。

pip download <package_name> --only-binary=:all: --platform manylinux1_x86_64 --python-version 36 --implementation cp --abi cp36m
替换<package_name>为你想下载的包名,并将其余参数设置为与目标环境相匹配的值。以上命令会尝试下载适用于Linux x86_64平台、Python 3.6、CPython实现的wheel包。

最后,如果你始终无法找到满足需求的wheel包,可能需要考虑从源码包构建wheel,如之前所述。

tar 包和 wheel 包之间如何相互转换

将 tar 包转换为 wheel 包:需要从源码构建 wheel。这通常涉及解压源码包,然后运行 setup.py bdist_wheel,在此之前,你需要确认所有构建依赖都已安装,并且你也需要安装 wheel 库。

tar -xzf package-name.tar.gz
cd package-name
pip install wheel  # 如果还没安装 wheel
python setup.py bdist_wheel

构建成功后,wheel 文件将位于 dist/ 目录中。

将 wheel 包转换为 tar 包:这个过程不太常见,因为 wheel 通常是已经建立好的包,它的目的是直接安装而不是作为源码发布。如果你有 wheel 文件并且需要源代码,通常的做法是找到对应的版本的源代码发布,而不是尝试从 wheel 转换。不过,如果你确实需要这样做,你可以手动解压 wheel 文件(它们实际上是 zip 文件),但这不会给你提供一个可用于重新打包的 setup.py 文件。

unzip package-name.whl -d destination_folder
请注意,wheel 格式是为了简化和加速包的安装,它们可能包含编译好的二进制文件,这些文件通常是针对特定平台构建的。源码包提供了更多的灵活性,因为它们可以在不同平台上从源码编译,但构建过程可能复杂,并且需要额外的时间和依赖。

setup.py的参数查询

python setup.py --help-commands

Standard commands:
  build             build everything needed to install
  build_py          "build" pure Python modules (copy to build directory)
  build_ext         build C/C++ extensions (compile/link to build directory)
  build_clib        build C/C++ libraries used by Python extensions
  build_scripts     "build" scripts (copy and fixup #! line)
  clean             clean up temporary files from 'build' command
  install           install everything from build directory
  install_lib       install all Python modules (extensions and pure Python)
  install_headers   install C/C++ header files
  install_scripts   install scripts (Python or otherwise)
  install_data      install data files
  sdist             create a source distribution (tarball, zip file, etc.)
  register          register the distribution with the Python package index
  bdist             create a built (binary) distribution
  bdist_dumb        create a "dumb" built distribution
  bdist_rpm         create an RPM distribution
  check             perform some checks on the package
  upload            upload binary package to PyPI

Extra commands:
  alias             define a shortcut to invoke one or more commands
  bdist_egg         create an "egg" distribution
  develop           install package in 'development mode'
  dist_info         create a .dist-info directory
  easy_install      Find/get/install Python packages
  editable_wheel    create a PEP 660 'editable' wheel
  egg_info          create a distribution's .egg-info directory
  install_egg_info  Install an .egg-info directory for the package
  rotate            delete older distributions, keeping N newest files
  saveopts          save supplied options to setup.cfg or other config file
  setopt            set an option in setup.cfg or another config file
  test              run unit tests after in-place build (deprecated)
  upload_docs       Upload documentation to sites other than PyPi such as devpi
  bdist_wheel       create a wheel distribution
posted @ 2019-12-17 23:40  零哭谷  阅读(4001)  评论(0编辑  收藏  举报