在sudo下运行pip install是否可以接受并且安全?

问题:在sudo下运行pip install是否可以接受并且安全?

我已经开始使用Mac来安装Python软件包,就像在工作中使用Windows PC一样。但是,在Mac上,我在写入日志文件或站点程序包时经常遇到权限被拒绝的错误。

因此,我考虑过pip install <package>sudosudo 下运行,但是考虑到我只是想将其安装在当前用户帐户下,是否安全/可接受地使用sudo?

日志文件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'

更新 这可能取决于权限,但是最好的方法是为您的python项目使用虚拟环境。sudo pip除非绝对必要,否则应避免运行。

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 denied errors while writing to log files or site-packages.

Therefore I thought about running pip install <package> under sudo but is that a safe/acceptable use of sudo considering I’m just wanting this to be installed under my current user account?

Example traceback from a logfile I/O error:

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'

Update This was likely down to permissions, however the best approach is to use virtual environments for your python projects. Running sudo pip should be avoided unless absolutely necessary.


回答 0

使用虚拟环境

$ virtualenv myenv
.. some output ..
$ source myenv/bin/activate
(myenv) $ pip install what-i-want

sudo当您要为全局的系统级Python安装安装内容时,才使用或提升权限。

最好使用虚拟环境为您隔离软件包。这样一来,您就可以畅玩而不会污染全局python安装。

另外,virtualenv不需要提升的权限。

Use a virtual environment:

$ virtualenv myenv
.. some output ..
$ source myenv/bin/activate
(myenv) $ pip install what-i-want

You only use sudo or elevated permissions when you want to install stuff for the global, system-wide Python installation.

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.

As a bonus, virtualenv does not need elevated permissions.


回答 1

它是可接受的安全运行pip installsudo

它不安全并且被皱着眉头–请参阅运行“ sudo pip”有什么风险? 要在主目录中安装Python软件包,您不需要root特权。见描述--user选项点子。

Is it acceptable & safe to run pip install under 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 description of --user option to pip.


回答 2

您最初的问题是pip无法将日志写入文件夹。

IOError: [Errno 13] Permission denied: '/Users/markwalker/Library/Logs/pip.log'

您需要将cd放入一个文件夹,在该文件夹中,调用的进程可以像/tmp这样写,cd /tmp然后重新调用该命令可能会起作用,但这不是您想要的。

实际上对于这种特殊情况(您不希望sudo用于安装python软件包)并且不需要全局软件包安装,可以使用如下--user标记:

pip install --user <packagename>

它会很好地工作。

我假设您具有一个用户python python安装,并且不想打扰有关virtualenv(不是很用户友好)或pipenv的阅读

正如评论部分中的某些人指出的那样,除非您不知道该怎么办并陷入困境,否则下一个方法不是一个好主意:

针对全局包的另一种方法例如您要执行的操作:

chown -R $USER /Library/Python/2.7/site-packages/

或更一般地

chown -R $USER <path to your global pip packages>

Your original problem is that pip cannot write the logs to the folder.

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 /tmp so a cd /tmp and re invoking the command will probably work but is not what you want.

BUT actually for this particular case (you not wanting to use sudo for installing python packages) and no need for global package installs you can use the --user flag like this :

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.

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 packages like 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>

回答 3

因为我遇到了同样的问题,所以我想强调一下,布莱恩·凯恩的第一条评论实际上是“ IOError:[Errno 13]”问题的解决方案:

如果在temp目录(cd /tmp)中执行,那么如果我运行IOError就不会再发生sudo pip install foo

Because I had the same problem, I want to stress that actually the first comment by Brian Cain is the solution to the “IOError: [Errno 13]”-problem:

If executed in the temp directory (cd /tmp), the IOError does not occur anymore if I run sudo pip install foo.


回答 4

virtualenvwrapper成功安装后,我在安装时遇到问题virtualenv

执行此操作后,我的终端抱怨:

pip install virtualenvwrapper

因此,我尝试失败(不推荐)

sudo pip install virtualenvwrapper

然后,我成功安装了它:

pip install --user virtualenvwrapper

I had a problem installing virtualenvwrapper after successfully installing virtualenv.

My terminal complained after I did this:

pip install virtualenvwrapper

So, I unsuccessfully tried this (NOT RECOMMENDED):

sudo pip install virtualenvwrapper

Then, I successfully installed it with this:

pip install --user virtualenvwrapper

回答 5

看来您的权限搞砸了。键入chown -R markwalker ~在终端和尝试pip一次?让我知道您是否已排序。

It looks like your permissions are messed up. Type chown -R markwalker ~ in the Terminal and try pip again? Let me know if you’re sorted.