Python软件包的安装(3种方法)

有些 Python 软件包是系统自带的,如 sys,这些包在安装 Python 时已自动安装。但有些包是需要自行下载安装的,如 PIL。这些第三方的软件有的以源代码的形式提供,有的以安装包的形式提供。

安装第三方 Python 包的方法有很多种。本节将介绍一些常见的方法。

1、复制源代码到系统目录

最简单的方法就是将 Python 源文件复制到 sys.path 包含的某个目录下。这样使用“imp ort文件名”即可导入该包。#价位@762459510 免费领取python、爬虫配套实操资料#

下面的代码查看 sys.path 变量包含的路径:


1.  >>> import sys
2.  >>> sys.path
3.  ['', '/anaconda3/lib/python37.zip',
4.  '/anaconda3/lib/python3.7',         # 这里要使用的目录
5.  '/anaconda3/lib/python3.7/lib-dynload',
6.  '/anaconda3/lib/python3.7/site-packages',
7.  '/anaconda3/lib/python3.7/site-packages/aeosa']


假定将文件 demo1.py 复制到目录 /anaconda3/lib/python3.7,就是上面的代码第 4 行指定的目录。该文件的内容如下:


1.  """ just a demo package
2.  """
3.  var_str = "love python"
4.  var_int = 88
5.  def show_usage():
6.  print("just to demo the package installation")


下面就可以使用 demo1 模块了,方法如下:


1.  >>> import demo1
2.  >>> demo1.var_int
3.  88
4.  >>> demo1.__doc__
5.  ' just a demo package\n'
6.  >>> demo1.var_str
7.  'love python'
8.  >>> demo1.show_usage()
9.  just to demo the package installation


2、使用PIP进行安装

最近比较流行的安装工具是 PIP,该工具一般在安装 Python 时已成功安装,不需要单独安装。 PIP 是客户端安装工具,安装包来自 PyPI,PIP 从 PyPI 上下载安装包,并且安装在本机。

Python 软件包的制作方需要有 PyPI 账户,这样才可以将自己编写的 Python 包推送到 PyPI 上以供其他用户使用。而对于普通用户来说,并不需要注册 PyPI 账户,只需要本地有 PIP 客户端程序即可。

PIP 提供了安装、卸载、列出安装软件包列表等功能。下面将对这些常见功能一一讲解。

1) 安装

安装的常用命令有两个,一个不指定版本信息,一个指定版本信息。下面分别是它们的格式:

pip install 包名
pip install 包名==版本号

如果已经有了一个安装包,但是希望修改其版本,则需要使用下面的命令:

pip install --upgrade 包名==版本

下面是安装一个 Python 包 lin-demo 的例子。

$ pip install lin-demo
Collecting lin-demo             # 获得lin-demo包的相关信息
Downloading https://files.pythonhosted.org/packages/87          #下载安装包
/35/985a9e7d7fd66bfe82c3c83092bb14d23105e15b5738e02b6c761737c8a8/
lin\_demo-0.0.1-py3-none-any.whl
Installing collected packages: lin-demo # 安装
Successfully installed lin-demo-0.0.1  # 安装成功,显示安装的版本

由于 PyPI 是一个开源项目,有些公司也在内部搭建了自己的 PyPI 服务器。有些专有的包可能只能从公司自己搭建的服务器上才能找到,这时就需要指定 PyPI 服务器信息,而不是使用默认的服务器。下面的命令可以指定安装包的来源:

pip install --index-url 包的来源 包名

如果一次要安装很多的软件包,尤其是在搭建开发或者运行环境时,这么一个一个地去执行安装命令进行安装显得不是很友好。

这时可以将要安装的包写在一个文本文件中,如 requirements.txt,然后使用该文本文件作为输入,将文本文件中列出的 Pyhton 包一次安装完成。这相当于是一个批处理命令,其格式如下:

pip install -r 包列表文件

这个包含包列表的文本文件的格式也很简单,一般一行写一个包的名和版本,格式还是“包名==版本”。下面便是一个例子:

CherryPy13.0.0
Jinja22.10
MySQL-python1.2.5
Routes2.4.1
SQLAlchemy1.1.15
Werkzeug0.13
hpc0.2.725
requests2.18.4

2) 卸载

卸载相对来说比较简单。由于一台机器对于某个软件包只能安装某一个版本,而且肯定是安装在本机上的,所以卸载命令没有指定版本、指定源这些参数。下面即为卸载某个包的命令格式:

pip uninstall 包名

3) 查看已经安装的包

有时在 import 语句时会抛出异常,原因可能是某个软件包没有安装,或者安装的版本不对。这时可以通过下面的命令来查看所有本地已经安装的 Python 包:

pip freeze

下面是一个例子:

$ pip freeze
alabaster0.7.12 # 0.7.12是版本信息
allure-pytest2.7.0
allure-python-commons2.7.0
anaconda-client1.7.2
… # 其他的已经安装的Python包
anaconda-navigator1.9.7
anaconda-project0.8.2
appnope==0.1.0

4) 查看某个包的详细情况

如果对某个包的具体作用不是很清楚,或者对其所依赖的包的情况不是很清楚,这时可以查看该包的详细描述,命令如下:

pip show 包名

下面是一个例子:

$ pip show SQLAlchemy
Name: SQLAlchemy
Version: 1.2.11 # 版本信息
Summary: Database Abstraction Library
Home-page: http://www.sqlalchemy.org
Author: Mike Bayer
Author-email: mike_mp@zzzcomputing.com
License: MIT License
Location: /Library/Python/2.7/site-packages # 安装位置
Requires: # 其所依赖的包
Required-by: # 依赖该包的包

5) 查看服务器上的包信息

可以使用命令行来查找包,命令行格式如下:

pip search 包名

然后便可以看到与指定包名类似的包的信息,包括版本和描述信息。下面是查找带有 demo 字样的包的信息:

$ pip search demo # 搜索与demo相关的Python包
demo (0.1.0) - egg test demo.
rattail-demo (0.1.0) - Rattail Software Demo
typosquating-demo (1.1.7) - Typosquating demo attack.
hacmec-demo (0.0.3) - hACMEc demo application
python-demo (0.0.3) - python scripts demos
python-demo (0.0.3) - python scripts demos
my-demo (2019.3.31) - A demo for python packaging.
pysp2tf-demo (0.11) - PySpark and TF demo
flask-demo (1.0.0) - demo template based on flask
agora-demo (0.1.0) - A demo testing configuration and dataset
management.
version-demo (0.0.3) - Just demo for checking how is version work
smooth-demo (0.1) - Tool to automate giving a demo on command line
pypi-task-demo (0.0.1) - PyPI demo
… # 省略一些包
jdhp-setuptools-demo (0.2) - A snippet to test setuptools and PyPI
flask_neglog (0.0.2) - demo
mylittlepypiproject (0.0.4) - A demo
flask_aide (0.0.1) - demo
mypypiproject (0.0.4) - A demo
my_little_pypi_project (0.0.4) - A demo
ccq18-hello (0.2) - demos
hykpyp (0.1) - this is demo
Flask-Kits (0.0.24) - demo
longj_demo (1.2.1) - this is a demo
demo-project-test-time (1.1111115) -
openerp-web-tests-demo (7.0.406) - Demonstration of web/javascript tests

当然,也可以在 PyPI 网站上进行查找。打开地址 https://pypi.org/,可以看到如图 1 所示的页面。最后,如果你的时间不是很紧张,并且又想快速的提高,最重要的是不怕吃苦,建议你可以联系维:762459510 ,那个真的很不错,很多人进步都很快,需要你不怕吃苦哦!大家可以去添加上看一下~

PyPI的主页面

图 1 PyPI的主页面

在搜索框内输入 demo,按 Enter 键,即可看到搜索到的与 demo 相关的 Python 包,如图 2 所示。

搜索demo相关的Python包

图 2 搜索 demo 相关的 Python 包

3、使用easy_install和setup进行安装

有些老的 Python 包可能没有放在 PyPI 服务器上,这时就需要使用 easy_install 来进行安装了。

使用 easy_install 安装其他软件包之前需要首先安装 easy_install 这个工具,步骤如下:

  1. 从 http://peak.telecommunity.com/dist/ 下载 ez_steup.py 到本地。
  2. 运行刚下载的 ez_setup.py。

这时在 Python 的系统目录下便可以看到文件 easy_install,这就是用来进行包安装的工具。现在直接运行命令“easy_install包名”即可安装需要的包。

有些模块的源代码中包含 setup.py 文件,这时一般使用下面的命令便可以完成安装任务:

python setup.py install

例如有一个 Python 包叫作 send2trash,其源代码地址是 https://github.com/hsoft/send2trash,下载源代码,然后可以使用源代码中的 setup.py 来进行安装。

本节介绍的安装方法并不常用,只有在安装某些特殊的包时才会用到。多数 Python 包都可以通过 PIP 进行安装,如本教程介绍的第三方 Python 包都是可以使用 PIP 进行安装的。
除。

关于Python技术储备

学好 Python 不论是就业还是做副业赚钱都不错,但要学会 Python 还是要有一个学习规划。最后大家分享一份全套的 Python 学习资料,给那些想学习 Python 的小伙伴们一点帮助!

包括:Python激活码+安装包、Python web开发,Python爬虫,Python数据分析,Python自动化测试学习等教程。带你从零基础系统性的学好Python!

👉[[CSDN大礼包:《python安装包&全套学习资料》免费分享]]安全链接,放心点击

一、Python学习大纲

Python所有方向的技术点做的整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
在这里插入图片描述

二、Python必备开发工具

在这里插入图片描述

三、入门学习视频

四、实战案例

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。在这里插入图片描述

五、python副业兼职与全职路线

在这里插入图片描述

上述这份完整版的Python全套学习资料已经上传CSDN官方,如果需要可以微信扫描下方CSDN官方认证二维码 即可领取

This is Python version 3.1.5 ============================ Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Python Software Foundation. All rights reserved. Python 3.x is a new version of the language, which is incompatible with the 2.x line of releases. The language is mostly the same, but many details, especially how built-in objects like dictionaries and strings work, have changed considerably, and a lot of deprecated features have finally been removed. Build Instructions ------------------ On Unix, Linux, BSD, OSX, and Cygwin: ./configure make make test sudo make install This will install Python as python3. You can pass many options to the configure script; run "./configure --help" to find out more. On OSX and Cygwin, the executable is called python.exe; elsewhere it's just python. On Mac OS X, if you have configured Python with --enable-framework, you should use "make frameworkinstall" to do the installation. Note that this installs the Python executable in a place that is not normally on your PATH, you may want to set up a symlink in /usr/local/bin. On Windows, see PCbuild/readme.txt. If you wish, you can create a subdirectory and invoke configure from there. For example: mkdir debug cd debug ../configure --with-pydebug make make test (This will fail if you *also* built at the top-level directory. You should do a "make clean" at the toplevel first.) What's New ---------- We try to have a comprehensive overview of the changes in the "What's New in Python 3.1" document, found at http://docs.python.org/3.1/whatsnew/3.1.html For a more detailed change log, read Misc/NEWS (though this file, too, is incomplete, and also doesn't list anything merged in from the 2.7 release under development). If you want to install multiple versions of Python see the section below entitled "Installing multiple versions". Documentation ------------- Documentation for Python 3.1 is online, updated twice a day: http://docs.python.org/3.1/ All documentation is also available online at the Python web site (http://docs.python.org/, see below). It is available online for occasional reference, or can be downloaded in many formats for faster access. The documentation is downloadable in HTML, PostScript, PDF, LaTeX (through 2.5), and reStructuredText (2.6+) formats; the LaTeX and reStructuredText versions are primarily for documentation authors, translators, and people with special formatting requirements. Converting From Python 2.x to 3.x --------------------------------- Python starting with 2.6 will contain features to help locating code that needs to be changed, such as optional warnings when deprecated features are used, and backported versions of certain key Python 3.x features. A source-to-source translation tool, "2to3", can take care of the mundane task of converting large amounts of source code. It is not a complete solution but is complemented by the deprecation warnings in 2.6. See http://docs.python.org/py3k/library/2to3.html for more information. Testing ------- To test the interpreter, type "make test" in the top-level directory. This runs the test set twice (once with no compiled files, once with the compiled files left by the previous test run). The test set produces some output. You can generally ignore the messages about skipped tests due to optional features which can't be imported. If a message is printed about a failed test or a traceback or core dump is produced, something is wrong. On some Linux systems (those that are not yet using glibc 6), test_strftime fails due to a non-standard implementation of strftime() in the C library. Please ignore this, or upgrade to glibc version 6. By default, tests are prevented from overusing resources like disk space and memory. To enable these tests, run "make testall". IMPORTANT: If the tests fail and you decide to mail a bug report, *don't* include the output of "make test". It is useless. Run the failing test manually, as follows: ./python Lib/test/regrtest.py -v test_whatever (substituting the top of the source tree for '.' if you built in a different directory). This runs the test in verbose mode. Installing multiple versions ---------------------------- On Unix and Mac systems if you intend to install multiple versions of Python using the same installation prefix (--prefix argument to the configure script) you must take care that your primary python executable is not overwritten by the installation of a different version. All files and directories installed using "make altinstall" contain the major and minor version and can thus live side-by-side. "make install" also creates ${prefix}/bin/python3 which refers to ${prefix}/bin/pythonX.Y. If you intend to install multiple versions using the same prefix you must decide which version (if any) is your "primary" version. Install that version using "make install". Install all other versions using "make altinstall". For example, if you want to install Python 2.5, 2.6 and 3.0 with 2.6 being the primary version, you would execute "make install" in your 2.6 build directory and "make altinstall" in the others. Issue Tracker and Mailing List ------------------------------ We're soliciting bug reports about all aspects of the language. Fixes are also welcome, preferable in unified diff format. Please use the issue tracker: http://bugs.python.org/ If you're not sure whether you're dealing with a bug or a feature, use the mailing list: python-dev@python.org To subscribe to the list, use the mailman form: http://mail.python.org/mailman/listinfo/python-dev/ Proposals for enhancement ------------------------- If you have a proposal to change Python, you may want to send an email to the comp.lang.python or python-ideas mailing lists for inital feedback. A Python Enhancement Proposal (PEP) may be submitted if your idea gains ground. All current PEPs, as well as guidelines for submitting a new PEP, are listed at http://www.python.org/dev/peps/. Release Schedule ---------------- See PEP 375 for release details: http://www.python.org/dev/peps/pep-0375/ Copyright and License Information --------------------------------- Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Python Software Foundation. All rights reserved. Copyright (c) 2000 BeOpen.com. All rights reserved. Copyright (c) 1995-2001 Corporation for National Research Initiatives. All rights reserved. Copyright (c) 1991-1995 Stichting Mathematisch Centrum. All rights reserved. See the file "LICENSE" for information on the history of this software, terms & conditions for usage, and a DISCLAIMER OF ALL WARRANTIES. This Python distribution contains *no* GNU General Public License (GPL) code, so it may be used in proprietary projects. There are interfaces to some GNU code but these are entirely optional. All trademarks referenced herein are property of their respective holders.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值