Chinaunix首页 | 论坛 | 博客
  • 博客访问: 418017
  • 博文数量: 114
  • 博客积分: 3361
  • 博客等级: 中校
  • 技术积分: 1060
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-18 13:14
文章分类

全部博文(114)

文章存档

2012年(1)

2011年(84)

2010年(29)

分类: Python/Ruby

2011-02-25 18:56:21

安装setuptools看到site-packages这个词,不解,放狗搜:


1. 确定site-packages目录,可用
  1. from distutils.sysconfig import get_python_lib
  2. print get_python_lib()
我机器得到的是:(ubuntu10.10)
  1. /usr/lib/python2.6/dist-packages
又有了dist-packages,这个问题等等在看,
python 的 site-packages 與 dist-packages
好鳥,如果你打算把你的 python script 包成 debian package,在 debian/rules 裡呼叫 setup.py 時,記得加上 --install-layout=deb。
如果不加,預設會把 package 裝到 site-packages 目錄下,但這個目錄是不在預設搜索packages路徑裡的。加上 --install-layout=deb 以後,才會裝到 dist-packages 下,這個路徑才會在預設搜索packages路徑裡。

想知道預設搜索packages路徑,可以先 import sys,再 print sys.path,就可以看到。



=========================================
吧pep0370给贴出来,方便查看

Abstract

This PEP proposes a new a per user site-packages directory to allow users the local installation of Python packages in their home directory.

Rationale

Current Python versions don't have a unified way to install packages into the home directory of a user (except for Mac Framework builds). Users are either forced to ask the system administrator to install or update a package for them or to use one of the many workarounds like Virtual Python [1], Working Env [2] or Virtual Env [3].

It's not the goal of the PEP to replace the tools or to implement isolated installations of Python. It only implements the most common use case of an additional site-packages directory for each user.

The feature can't be implemented using the environment variable PYTHONPATH. The env var just inserts a new directory to the beginning of sys.path but it doesn't parse the pth files in the directory. A full blown site-packages path is required for several applications and Python eggs.

Specification

site directory (site-packages)

A directory in sys.path. In contrast to ordinary directories the pth files in the directory are processed, too.

user site directory

A site directory inside the users' home directory. A user site directory is specific to a Python version. The path contains the version number (major and minor only).

Unix (including Mac OS X)~/.local/lib/python2.6/site-packagesWindows%APPDATA%/Python/Python26/site-packages

user data directory

Usually the parent directory of the user site directory. It's meant for Python version specific data like config files, docs, images and translations.

Unix (including Mac)~/.local/lib/python2.6Windows%APPDATA%/Python/Python26

user base directory

It's located inside the user's home directory. The user site and use config directory are inside the base directory. On some systems the directory may be shared with 3rd party apps.

Unix (including Mac)~/.localWindows%APPDATA%/Python

user script directory

A directory for binaries and scripts. [10] It's shared across Python versions and the destination directory for scripts.

Unix (including Mac)~/.local/binWindows%APPDATA%/Python/Scripts
Windows Notes

On Windows the Application Data directory (aka APPDATA) was chosen because it is the most designated place for application data. Microsoft recommands that software doesn't write to USERPROFILE [5] and My Documents is not suited for application data, either. [8] The code doesn't query the Win32 API, instead it uses the environment variable %APPDATA%.

The application data directory is part of the roaming profile. In networks with domain logins the application data may be copied from and to the a central server. This can slow down log-in and log-off. Users can keep the data on the server by e.g. setting PYTHONUSERBASE to the value "%HOMEDRIVE%%HOMEPATH%Applicata Data". Users should consult their local adminstrator for more information. [13]

Unix Notes

On Unix ~/.local was chosen in favor over ~/.python because the directory is already used by several other programs in analogy to /usr/local[7] [11]

Mac OS X Notes

On Mac OS X Python uses ~/.local directory as well. [12] Framework builds of Python include~/Library/Python/2.6/site-packages as an additional search path.

Implementation

The site module gets a new method adduserpackage() which adds the appropriate directory to the search path. The directory is not added if it doesn't exist when Python is started. However the location of the user site directory and user base directory is stored in an internal variable for distutils.

The user site directory is added before the system site directories but after Python's search paths andPYTHONPATH. This setup allows the user to install a different version of a package than the system administrator but it prevents the user from accidently overwriting a stdlib module. Stdlib modules can still be overwritten withPYTHONPATH.

For security reasons the user site directory is not added to sys.path when the effective user id or group id is not equal to the process uid / gid [9]. It's an additional barrier against code injection into suid apps. However Python suid scripts must always use the -E and -s option or users can sneak in their own code.

The user site directory can be suppressed with a new option -s or the environment variable PYTHONNOUSERSITE. The feature can be disabled globally by setting site.ENABLE_USER_SITE to the value False. It must be set by editing site.py. It can't be altered in sitecustomize.py or later.

The path to the user base directory can be overwritten with the environment variable PYTHONUSERBASE. The default location is used when PYTHONUSERBASE is not set or empty.

distutils.command.install (setup.py install) gets a new argument --user to install packages in the user site directory. The required directories are created on demand.

distutils.command.build_ext (setup.py build_ext) gets a new argument --user which adds the include/ and lib/ directories in the user base dirctory to the search paths for header files and libraries. It also adds the lib/ directory to rpath.

The site module gets two arguments --user-base and --user-site to print the path to the user base or user site directory to the standard output. The feature is intended for scripting, e.g. ./configure --prefix$(python2.5 -m site --user-base)

distutils.sysconfig will get methods to access the private variables of site. (not yet implemented)

The Windows updater needs to be updated, too. It should create an menu item which opens the user site directory in a new explorer windows.

Reference Implementation

A reference implementation is available in the bug tracker. [4]

References
[1]Virtual Python 
[2]Working Env  http://blog.ianbicking.org/workingenv-revisited.html
[3]Virtual Env 
[4]reference implementation 
[5]MSDN: CSIDL 
[6]Initial suggestion for a per user site-packages directoryhttp://permalink.gmane.org/gmane.comp.python.devel/90902
[7]Suggestion of ~/.local/ http://permalink.gmane.org/gmane.comp.python.devel/90925
[8]APPDATA discussion http://permalink.gmane.org/gmane.comp.python.devel/90932
[9]Security concerns and -s option http://permalink.gmane.org/gmane.comp.python.devel/91063
[10]Discussion about the bin directory http://permalink.gmane.org/gmane.comp.python.devel/91095
[11]freedesktop.org XGD basedir specs mentions ~/.local
[12]~/.local for Mac and usercustomize file http://permalink.gmane.org/gmane.comp.python.devel/91167
[13]Roaming profile on Windows http://permalink.gmane.org/gmane.comp.python.devel/91187




======================================
另一日志,原味链接在文章开始

有時候需要 import 一些自己的script,但是還不想寫成module,
找了一下資料,Python2.6 有實作了 Per User site-packages,不過
目前系統用的還是 Python2.5所以就需要一些另外的作法。


Python2.5

直接寫 .pth 丟到  /usr/lib/python2.5/site-packages/
.pth 內容就是你的script的位置,
不過這樣需要動用到  root權限

或者用python scription

1import sys
2sys.path.insert(0,"~/.local/python")

Python2.6

方法一:

  • Unix and Mac OS X: ~/.local/
  • Windows: %APPDATA%/Python

系統會找出這些目錄下面的 site-packages的目錄,然後加入 sys.path

方法二:

設系統變數 PYTHONUSERBASE

要取消設定的話,設定另一變數 PYTHONNOUSERSITE

export PYTHONUSERBASE=』~/local/python』

方法三:

The feature can be disabled entirely by running Python with the -s option or setting thePYTHONNOUSERSITE environment variable.

Reference:

  1. site — Site-specific configuration hook
  2. PEP 370 – Per-user site-packages Directory





阅读(6630) | 评论(0) | 转发(0) |
0

上一篇:我的emacs配置

下一篇:trac安装的输出

给主人留下些什么吧!~~