codecamp

10.13 安装私有的包

问题

You want to install a third-party package, but you don’t have permission to install packagesinto the system Python. Alternatively, perhaps you just want to install a packagefor your own use, not all users on the system.

解决方案

Python has a per-user installation directory that’s typically located in a directory suchas ~/.local/lib/python3.3/site-packages. To force packages to install in this directory, givethe –user option to the installation command. For example:

python3 setup.py install --user

or

pip install --user packagename

The user site-packages directory normally appears before the system site-packages directoryon sys.path. Thus, packages you install using this technique take priority overthe packages already installed on the system (although this is not always the case dependingon the behavior of third-party package managers, such as distribute or pip).

讨论

Normally, packages get installed into the system-wide site-packages directory, which isfound in a location such as /usr/local/lib/python3.3/site-packages. However, doing sotypically requires administrator permissions and use of the sudo command. Even if youhave permission to execute such a command, using sudo to install a new, possibly unproven,package might give you some pause.

Installing packages into the per-user directory is often an effective workaround thatallows you to create a custom installation.

As an alternative, you can also create a virtual environment, which is discussed in thenext recipe.

10.12 导入模块的同时修改模块
10.14 创建新的Python环境
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }