简体   繁体   English

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

[英]Is it acceptable and safe to run pip install under sudo?

I've started to use my Mac to install Python packages in the same way I do with my Windows PC at work; 我已经开始使用Mac来安装Python软件包,就像在工作Windows PC时一样。 however on my Mac I've come across frequent permission denied errors while writing to log files or site-packages. 但是在Mac上,我在写入日志文件或站点程序包时经常遇到权限被拒绝的错误。

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? 因此,我考虑过在sudo下运行pip install <package> ,但是考虑到我只是想将其安装在当前用户帐户下,这是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'

Update This was likely down to permissions, however the best approach is to use virtual environments for your python projects. 更新这可能取决于权限,但是最好的方法是为您的python项目使用虚拟环境。 Running sudo pip should be avoided unless absolutely necessary. 除非绝对必要,否则应避免运行sudo pip

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. 当您要为全局,系统范围的Python安装安装内容时,仅使用sudo或提升的权限。

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不需要提升的权限。

Is it acceptable & safe to run pip install under sudo ? sudo下运行pip install是否可以接受并且安全?

It's not safe and it's being frowned upon – see What are the risks of running 'sudo pip'? 它不安全并且被皱着眉头–请参阅运行“ sudo pip”有什么风险? To install Python package in your home directory you don't need root privileges. 要在主目录中安装Python软件包,您不需要root特权。 See description of --user option to pip. 请参阅--user选项的说明

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 /tmp so a cd /tmp and re invoking the command will probably work but is not what you want. 您需要将cd放入一个文件夹,在该文件夹中,被调用的进程可以像/tmp一样编写,因此cd /tmp并重新调用该命令可能会起作用,但这不是您想要的。

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 : 实际上对于这种特定情况(您不想使用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 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>

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: 因为我有同样的问题,所以我想强调一下, 布莱恩·凯恩的第一条评论实际上是“ IOError:[Errno 13]”问题的解决方案:

If executed in the temp directory ( cd /tmp ), the IOError does not occur anymore if I run sudo pip install foo . 如果在temp目录( cd /tmp )中执行,那么如果我运行sudo pip install foo ,就不会再发生IOError。

I had a problem installing virtualenvwrapper after 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 successfully installed it with this: 然后,我成功安装了它:

pip install --user virtualenvwrapper

It looks like your permissions are messed up. 您的权限似乎混乱了。 Type chown -R markwalker ~ in the Terminal and try pip again? 在终端中输入chown -R markwalker ~并再次尝试pip Let me know if you're sorted. 让我知道您是否已排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM