python查询是否有python package 安装

苹果/安卓/wp
积分 1056, 距离下一级还需 319 积分
权限: 自定义头衔, 签名中使用图片, 隐身
道具: 彩虹炫, 涂鸦板, 雷达卡, 热点灯, 金钱卡, 显身卡, 匿名卡, 抢沙发下一级可获得
权限: 设置帖子权限道具: 提升卡
购买后可立即获得
权限: 隐身
道具: 金钱卡, 彩虹炫, 雷达卡, 热点灯, 涂鸦板
开心签到天数: 241 天连续签到: 1 天[LV.8]以坛为家I
我用pip安装pygameui:
首先在PyPi下载pygameui 0.2.0的安装包,解压
然后再cmd上运行python setup.py install 安装,但是结果提示:
error: Could not find suitable distribution for Requirement.parse ('pygame&=1.9.1')
请问这是什么原因呢?应该怎么解决??
支持楼主:、
购买后,论坛将把您花费的资金全部奖励给楼主,以表示您对TA发好贴的支持
载入中......
本帖被以下文库推荐
& |主题: 10, 订阅: 1
然后我重新安装一遍,结果又提示:
error: Setup script exited with error: Unable to find vcvarsall.bat
这又是什么原因?
vcvarsall.bat又是什么??
楼主可以安装一个一个python(x,y)啊,很多常用的包都有了,还很方便
lxy444 发表于
楼主可以安装一个一个python(x,y)啊,很多常用的包都有了,还很方便这是什么??
xucaifeng66 发表于
这是什么??相当于一个集成环境吧,你可以用一下
python(x,y)这个??
xucaifeng66 发表于
python(x,y)这个??
里面有个pythonxy
但我不清楚它是否包含你想要的东西?
pygame不支持你的python版本。
楼主应该是用3.x,我记得pygame值更新到2.7
xiaoxiaoyu 发表于
pygame不支持你的python版本。
楼主应该是用3.x,我记得pygame值更新到2.7我是用的2.7, 还是没搞明白,不过有可能是pygame是32位的,要求python也是32位版本的,有时间再试试
无限扩大经管职场人脉圈!每天抽选10位免费名额,现在就扫& 论坛VIP& 贵宾会员& 可免费加入
&nbsp&nbsp|
&nbsp&nbsp|
&nbsp&nbsp|
&nbsp&nbsp|
&nbsp&nbsp|
&nbsp&nbsp|
如有投资本站或合作意向,请联系(010-);
邮箱:service@pinggu.org
投诉或不良信息处理:(010-)
京ICP证090565号
论坛法律顾问:王进律师模块间相互独立相互引用是任何一种编程语言的基础能力。对于&模块&这个词在各种编程语言中或许是不同的,但我们可以简单认为一个程序文件是一个模块,文件里包含了类或者方法的定义。对于编译型的语言,比如C#中的一个.cs文件,Java中的一个.java或者编译后的.class文件可以认为是一个模块(但常常不表述为模块);对于解释型的语言会更加直观些,比如PHP的.php文件,在Python中就是.py文件可以认为是一个模块。在&模块&之上有&包&,主要是为了方便组织和管理模块。比如C#中编译后的.dll文件(但常常不表述为包Package,而是库Library),Java将.class打包后的.jar文件,PHP的.phar文件(模仿Java包),在Python中一个特殊定义的文件夹是一个包,可以打包为egg文件。但对于解释型语言&包&并没有编译成低级语言而后打包的意思,只是更加方便模块化和管理模块间的依赖。每种编程语言对于模块和包管理都有一定的约定,不了解这些约定,那会给学习这种语言的带来障碍。下面我想来梳理一下Python的这些约定。
一、Python查找模块的路径
运行Python应用或引用Python模块,Python解释器要有一个查找的过程。可以通过设置一个环境变量PYTHONPATH为Python增加一个搜索路径,以方便查找到相关Python模块(不同的操作系统环境变量的设置稍有不同,默认以下都是WIndows环境),这与众多应用程序需要设置一个系统环境变量的道理是一样的。在命令行中可以通过以下命令设置:
C:\Users\Administrator&set PYTHONPATH=E:/Project/Python/ModuleAndPackage/
进入Python环境后可以,通过Python的sys.path属性获得当前搜索路径的配置,可以看到之前我们设置的路径已经在当前搜索路径中了。
C:\Users\Administrator&python
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec
5 :19) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
&&& import sys
&&& sys.path
['', 'E:\\Project\\Python\\ModuleAndPackage', 'C:\\Windows\\system32\\python27.zip', 'C:\\Python\\DLLs', 'C:\\Python\\lib', 'C:\\Python\\lib\\plat-win', 'C:\\Python\\lib\\lib-tk', 'C:\\Python', 'C:\\Python\\lib\\site-packages']
也可以通过sys模块的append方法在Python环境中增加搜索路径。
&&& sys.path.append("E:\\Project\\Python\\ModuleAndPackage2")
&&& sys.path
['', 'E:\\Project\\Python\\ModuleAndPackage', 'C:\\Windows\\system32\\python27.zip', 'C:\\Python\\DLLs', 'C:\\Python\\lib', 'C:\\Python\\lib\\plat-win', 'C:\\Python\\lib\\lib-tk', 'C:\\Python', 'C:\\Python\\lib\\site-packages', 'E:\\Project\\Python\\ModuleAndPackage2']
二、Python中的模块和包
前面已经提到每个.py文件都是可以认为是一个Python模块,.py文件中可以包含类、方法、变量和常量(Python还没有严格意义上的常量,只是约定大写的变量作为常量),文件内也可以直接写所有的逻辑语句并在加载时从上之下直接执行,这与其他解释型语言是类似的。例如我们选择在文件夹ModuleAndPackage中创建一个文本文件person.py文件即创建了一个简单的Python模块,其内容如下:
# -*- coding: utf-8 -*-
"This person"
print name
def say(something):
print name,'says', something
那么接下来我们就可以在Python环境中执行person.py。我们可以直接像执行一个批处理文件那样执行person.py,在cmd命令行输入:
Python E:/Project/Python/ModuleAndPackage/person.py
本质上任何一个Python应用的入口模块都是这样被执行的(像C#和Java中的main函数),但是引用一个模块,就要建立运行它的上下文环境。我们先设置一个环境变量PYTHONPATH,以便Python解释器找到person.py模块,然后import person模块,即可访问其中的方法或变量。
C:\Users\Administrator&python
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec
5 :19) [MSC v.1500 32 bit (
Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
&&& import person
This person
&&& person.say("hello")
This person says hello
&&& print person.name
This person
Python需要去某些固定的路径下去查找Python模块,上面我们设置在ModuleAndPackage中查找。但是这些路径下也是有目录层次的,Python是如何查找子目录中的模块呢?特别是引用第三方包时,我们也需要知道一定的层次关系。实际上,Python通过目录和文件构建包结构,并且包是层层嵌套的,和目录层层嵌套是一样的,这样就构成了包内的访问路径(或者命名空间,也可以说Python应用的命名空间与其目录和文件结构是对应了,似乎缺少了一些灵活,但也更简单)。例如我们在ModuleAndPackage文件夹下,创建一个文件夹animal,里面创建一个文本文件pet.py,其内容如下:
# -*- coding: utf-8 -*-
"This pet"
print name
def run(somewhere):
print name,'runs', somewhere
那么如何引用pet.py这个模块呢?按照Python的约定,需要在animal文件夹中创建名为__init__.py的空文本文件,以标识animal文件夹是一个包。倘若animal文件夹内还有文件夹作为包,也必须包含__init__.py文件。这样就层层标识了访问的路径。
&&& import animal.pet
&&& print animal.pet.name
&&& animal.pet.run("everywhere")
This pet runs everywhere
或者使用from关键字直接导入模块内的属性或方法:
&&& from animal.pet import name,run
&&& print name
&&& run("everywhere")
This pet runs everywhere
三、Python模块间引用
简答来说,只要Python模块在其执行环境配置的搜索路径中,并且其所在位置是包结构的一部分,那么我们就可以引用该模块。上文已经提供了模块引用的基本示例。只不过模块间引用时import语句是写在模块文件中,我们修改person.py模块的代码。
1、from、import和as
# -*- coding: utf-8 -*-
"This person"
print name
def say(something):
print name,'says', something
from animal.pet import name as pet_name, run as pet_run
def have():
print name,'has', pet_name
import语句可以写在文档中的任何位置,甚至if语句中,以便更好的控制模块引用。还可以通过as语句,使用另一个变量名进行引用,以避免变量名冲突。
&&& import person
This person
&&& print person.name
This person
&&& print person.pet_name
&&& person.have()
This person has This pet
2、*通配符
上面的import代码明确了引用的变量名,但如果想引用模块中所有变量可以使用*通配符,将上面的import语句改写如下:
from animal.pet import *
但这样有可能造成变量名冲突,如下name变量发生冲突,覆盖了person自己的name变量的值:
&&& import person
This person
&&& print person.name
但如果想用*通配符,又不想引用模块中的所有变量,可以在模块中用变量__all__进行限制,修改pet.py,限制只引用ID和run两个变量名。
# -*- coding: utf-8 -*-
__all__ = ['ID','run']
"This pet"
print name
def run(somewhere):
print name,'runs', somewhere
因为没有引用pet模块中的name变量,person的name变量值没有改变,run却可以调用了。
&&& import person
This person
&&& print person.name
This person
&&& person.run("nowhere")
This pet runs nowhere
上面都是引用具体的animal.pet模块,但是这对于一个相对独立且拥有众多的模块的包来说就显得麻烦了,可以直接import animal吗?答案是肯定的,但是Python不像C#引用dll或者java引用jar那样,引用后包内的模块就可以通过命名空间直接访问了(在访问控制许可下)。默认情况下Python还是需要导入包内的具体模块的,但有个变通的办法,就是使用包中__init__.py文件,提前准备包内需要被引用的各个模块中的变量,类似于向外部引用者暴露包内接口。__init__.py文件代码是在包或者包内模块被引用时执行的,因而可以在其中做一些初始化的工作。修改animal文件夹中__init__.py文件如下,其中模块可以使用绝对路径和相对路径,相对路径中一个句点.代表同级目录,两个句点..代表父目录。
print "__init__"
from pet import name as pet_name, run as pet_run
#from animal.pet import name as pet_name, run as pet_run
#from .pet import name as pet_name, run as pet_run
修改person.py,直接引用anmial包:
# -*- coding: utf-8 -*-
"This person"
print name
def say(something):
print name,'says', something
import animal
def have():
print name,'has', pet_name
在Python环境中引用person模块,person引用animal,并自动执行__init__的代码加载相关变量,通过dir方法可以查看模块中的变量,其中两个下划线开始的变量每个模块都有,这些变量具有特殊的作用,是Python预定义的。
&&& import person
This person
&&& dir(person)
['ID', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'have',
'name', 'pet', 'pet_name', 'pet_run', 'say']
&&& print person.pet_name
&&& person.pet_run("nowhere")
This pet runs nowhere
阅读(...) 评论()ResourcePackage for Python
ResourcePackage 1.0.0 for Python
ResourcePackage is a mechanism for automatically managing
"resources"
(i.e. non-Python files: small images, documentation files, binary data)
embedded in Python modules (as Python source code), particularly for
those wishing to create re-usable Python packages which require their
own resource-sets. You can download it from the project page
ResourcePackage allows you to set up resource-specific sub-packages
within your package. It creates a Python module for each resource
placed in the resource package's directory during development. You can
set up these packages simply by copying an __init__.py file and then
the resources saved/updated in the package directory like so:
from mypackage.resources import open_icoresult = myStringLoadingFunction( open_ico.data )
ResourcePackage scans the package-directory on import to refresh
module contents, so simply saving an updated version of the file will
make it available the next time your application is run.
When you are ready to distribute your package, you need only replace
the copied __init__.py file with a dummy __init__.py to disable the
scanning support and eliminate all dependencies on resourcepackage
is, your users do not need to have resourcepackage installed once this
Users of your packages do not need to do anything special when
creating their applications to give you access to your resources, as
they are simply Python packages/modules included in your package's
hierarchy. Your package's code (other than the mentioned __init__.py)
doesn't change.& Similarly, automatic packaging systems will pick
up your resources as regular Python modules being imported by your
source-code.
There are two utility scripts, extract.py and scan.py which can be
used to manually extract or embed resources in a resourcepackage
even if the package no longer has a resourcepackage-aware __init__.py
file. &See these scripts for usage details.
ResourcePackage has been stable for many months now.& There
is, at the moment, nothing more planned to be added or changed, as it
fulfills all of my needs for it.& I am, of course, interested in suggestions you might
have for improving the package.& You can
get the distribution from the project page at:
Happy Packaging!
Usage Details
During development:
copy scanning__init__.py to __init__.py in your resource directory
[optionally] edit __init__.py to customise operation (examples):
use resourcepackage.wxgenerators (or another generator package)
to provide special handling for particular file-types
set the "force" flag to always reload resources into their
corresponding modules
save your resources to the resource directory
on (re-)import of __init__.py, it will scan your resource
directory and update resource files with dates newer than their
corresponding python module
import your resources as Python modules and access their binary
content as package.module.data
name will be the name of the file (with extension) with spaces,
'.' and '-' characters converted to '_' characters.
Note: in version 1.0.0a2 and earlier, module names
dropped the file extension and lowercased the entire module name.
&In 1.0.0a3 and forward neither of these transformations is
performed. &As a result, "Index.html" will be show up in the
Index_html, instead of index in all newer version. &Also as a
result, Index.html and index.html will be recognised as the same name
on case-ignoring filesystems, but if you've already got the source
on a case-ignoring system, they can't both exist :) .
When wanting to distribute your package:
Replace __init__.py with your run-time __init__.py (which can
just be an empty Python module, as with any Python package, which
doesn't need to know anything about ResourcePackage). &
That eliminates all connections to ResourcePackage
and you now have a regular Python package with all of your resources
embedded in it's modules. &This isn't technically necessary, but
will make the installation cleaner and more efficient for users of your
Reference Documentation
available online for those wishing to browse it.
There have been a few changes in ResourcePackage during the
development period. &In particular:
version 1.0.0 adds a few Unix #! lines to the extract and scan
version 1.0.0b1 introduces an encoding comment at the top of
generated files.& This avoids a spurious warning from Python 2.3b2
and above indicating that non-ASCII characters are being included in a
version 1.0.0a3 had some significant incompatible changes from
version 1.0.0a2. &If you downloaded ResourcePackage before
you should expect some significant breakage, as the naming scheme for
modules, as well as the mechanisms for use in scanning__init__.py
changed significantly. &See readme.txt in the source archive for怎样查找和安装python的包
来自: (念起即觉,觉即不随)
看《python科学计算》,看到Traits这一章的时候,发现安装的python(x,y)并没有enthought的api。不知道python下怎样安装和查找包,望知道的指点一下。谢谢!
推荐到广播
80220 人聚集在这个小组
(肥肥爱吃肉呀)
(赤子如君)
第三方登录:16439人阅读
python(1)
小技巧(2)
用python以来,发现python的各种包在不同平台上经常出现各种安装问题。所以各种安装方法基本试了一遍,下面就把它们都记录一下。各位轮番尝试,说不定某种方法就成功了呢…
最原始的是通过源代码进行安装,在package主页上下载.tar.gz压缩包,解压之后,进入主目录,输入:
python setup.py install
就可以通过安装包的setup.py代码进行安装。然而由于c语言库等问题,在编译时常出现各种错误。而且不方便进行版本更新,所以更为推荐的方法是利用包管理工具进行安装。
包管理工具有两种,easy_install和pip。现在easy_install已经基本被淘汰掉。建议大家用pip工具管理自己的python安装包。
不论是win,mac还是linux,用pip的安装方法基本都是:
pip install &package-name&
除了个别包的安装语句有特殊要求或安装package的名称与使用时不同,如sklearn是:
pip install -U scikit-learn
这时去其package主页上查明就好了。
对于很多绝大多数常用package,这个提供了其预编译好的安装文件。感觉uci.edu这个学校热衷于提供各种开放下载的代码和数据,挺有意思。
如果前面的方法都安装不好的话,就去那个连接上去找找看有没有预编译好的安装程序吧。对于win平台,早期大多数包都提供了.exe安装文件,直接点击安装就自动查找目录安装。如果报错的话,就去查查看你是不是下错了对应python和win版本。而后期的预编译文件大都是.whl文件,这种文件通过pip照样能够安装。
下载完.whl文件后,由命令行(以管理员身份)进入.whl所在目录,输入:
pip install &package-name&.whl
就可以安装了。
另附pip包管理工具的更新方式。这个工具很有意思,会自动检查自己的最新版本,然后在你使用它的时候告知你需要更新,更新方式就是:
pip install --upgrade pip
&&相关文章推荐
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:193518次
积分:1384
积分:1384
排名:千里之外
原创:15篇
(1)(1)(1)(1)(12)

我要回帖

更多关于 python package 路径 的文章

 

随机推荐