Python 在 sudo 下运行 pip install 是否可以接受和安全?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15028648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it acceptable and safe to run pip install under sudo?
提问by markwalker_
I've started to use my Mac to install Python packages in the same way I do with my Windows PC at work; however on my Mac I've come across frequent permission deniederrors while writing to log files or site-packages.
我已经开始使用我的 Mac 来安装 Python 包,就像我在工作时使用 Windows PC 一样;但是在我的 Mac 上,我在写入日志文件或站点包时经常遇到权限被拒绝的错误。
Therefore I thought about running pip install <package>under sudobut is that a safe/acceptable use of sudo considering I'm just wanting this to be installed under my current user account?
因此,我考虑pip install <package>在下运行,sudo但考虑到我只是希望将其安装在我当前的用户帐户下,这是安全/可接受的 sudo 使用吗?
Example traceback from a logfile I/O error:
来自日志文件 I/O 错误的示例回溯:
Command /usr/bin/python -c "import setuptools;__file__='/Users/markwalker/build/pycrypto/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --single-version-externally-managed --record /var/folders/tq/hy1fz_4j27v6rstzzw4vymnr0000gp/T/pip-k6f2FU-record/install-record.txt failed with error code 1 in /Users/markwalker/build/pycrypto
Storing complete log in /Users/markwalker/Library/Logs/pip.log
Traceback (most recent call last):
File "/usr/local/bin/pip", line 8, in <module>
load_entry_point('pip==1.1', 'console_scripts', 'pip')()
File "/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg/pip/__init__.py", line 116, in main
return command.main(args[1:], options)
File "/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg/pip/basecommand.py", line 141, in main
log_fp = open_logfile(log_fn, 'w')
File "/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg/pip/basecommand.py", line 168, in open_logfile
log_fp = open(filename, mode)
IOError: [Errno 13] Permission denied: '/Users/markwalker/Library/Logs/pip.log'
UpdateThis was likely down to permissions, however the best approach is to use virtual environments for your python projects. Running sudo pipshould be avoided unless absolutely necessary.
更新这可能取决于权限,但是最好的方法是为您的 Python 项目使用虚拟环境。sudo pip除非绝对必要,否则应避免跑步。
采纳答案by Burhan Khalid
Use a virtual environment:
使用虚拟环境:
$ virtualenv myenv
.. some output ..
$ source myenv/bin/activate
(myenv) $ pip install what-i-want
You only use sudoor elevated permissions when you want to install stuff for the global, system-wide Python installation.
仅sudo当您想要为全局、系统范围的 Python 安装安装内容时,才使用或提升权限。
It is best to use a virtual environment which isolates packages for you. That way you can play around without polluting the global python install.
最好使用为您隔离包的虚拟环境。这样你就可以在不污染全局 python 安装的情况下玩耍。
As a bonus, virtualenv does not need elevated permissions.
作为奖励,virtualenv 不需要提升权限。
回答by hd1
It looks like your permissions are messed up. Type chown -R markwalker ~in the Terminal and try pipagain? Let me know if you're sorted.
看起来你的权限搞砸了。键入chown -R markwalker ~在终端和尝试pip一次?让我知道你是否被排序。
回答by Edgar
Because I had the same problem, I want to stress that actually the first comment by Brian Cainis the solution to the "IOError: [Errno 13]"-problem:
因为我遇到了同样的问题,我想强调的是,实际上Brian Cain的第一条评论是“IOError: [Errno 13]”问题的解决方案:
If executed in the temp directory (cd /tmp), the IOError does not occur anymore if I run sudo pip install foo.
如果在临时目录 ( cd /tmp) 中执行,则在运行sudo pip install foo.
回答by Piotr Dobrogost
Is it acceptable & safe to run
pip installundersudo?
pip install在 下运行是否可以接受且安全sudo?
It's not safe and it's being frowned upon – see What are the risks of running 'sudo pip'?To install Python package in your home directory you don't need root privileges. See descriptionof --useroption to pip.
它不安全,而且令人不悦——请参阅运行“sudo pip”有哪些风险?要在您的主目录中安装 Python 包,您不需要 root 权限。见描述的--user选项点子。
回答by throws_exceptions_at_you
Your original problem is that pip cannot write the logs to the folder.
您的原始问题是 pip 无法将日志写入文件夹。
IOError: [Errno 13] Permission denied: '/Users/markwalker/Library/Logs/pip.log'
You need to cd into a folder in which the process invoked can write like /tmpso a cd /tmpand re invoking the command will probably work but is not what you want.
您需要 cd 进入一个文件夹,在该文件夹中调用的进程可以像/tmp这样写入,cd /tmp然后重新调用该命令可能会起作用,但不是您想要的。
BUTactually for this particular case (you not wanting to use sudofor installing python packages) and no need for global package installs you can use the --userflag like this :
但实际上对于这种特殊情况(您不想sudo用于安装 python 包)并且不需要全局包安装,您可以使用这样的--user标志:
pip install --user <packagename>
and it will work just fine.
它会工作得很好。
I assume you have a one user python python installation and do not want to bother with reading about virtualenv(which is not very userfriendly) or pipenv.
我假设您有一个单用户 python python 安装,并且不想费心阅读有关virtualenv(不是非常用户友好)或pipenv。
As some people in the comments section have pointed out the next approach is not a very good idea unless you do not know what to do and got stuck:
正如评论部分中的一些人指出的那样,除非您不知道该怎么做并且卡住了,否则下一种方法不是一个好主意:
Another approach for global packageslike in your case you want to do something like :
全局包的另一种方法,例如在您的情况下,您想要执行以下操作:
chown -R $USER /Library/Python/2.7/site-packages/
or more generally
或更一般地
chown -R $USER <path to your global pip packages>
回答by Let Me Tink About It
I had a problem installing virtualenvwrapperafter successfully installing virtualenv.
virtualenvwrapper成功安装后我在安装时遇到问题virtualenv。
My terminal complained after I did this:
执行此操作后,我的终端抱怨:
pip install virtualenvwrapper
So, I unsuccessfully tried this (NOT RECOMMENDED):
所以,我没有成功尝试这个(不推荐):
sudo pip install virtualenvwrapper
Then, I successfullyinstalled it with this:
然后,我成功地安装了它:
pip install --user virtualenvwrapper

