简体   繁体   English

Pip 默认行为与 virtualenv 冲突?

[英]Pip default behavior conflicts with virtualenv?

I was following this tutorial我正在关注本教程

When I got to virtualenv flask command, I received this error message:当我到达virtualenv flask命令时,我收到了以下错误消息:

Can not perform a '--user' install. User site-packages are not visible in this virtualenv.

This makes sense as the point of virtualenv is to create a new environment which you can control, and the --user command places everything in a specific location, defeating the objective of separation of dev environment.这是有道理的,因为 virtualenv 的目的是创建一个您可以控制的新环境,而--user命令将所有内容放在特定位置,从而破坏了分离开发环境的目标。

It seems like pip defaults to --user installations though, can I change this default behavior?似乎 pip 默认为--user安装,我可以更改此默认行为吗? And, even better, can I get pip to play nice with virtualenv at all times?而且,更好的是,我可以让 pip 始终与 virtualenv 一起玩吗?

To clarify, here is what my terminal looks like.为了澄清,这是我的终端的样子。

MELCHIOR:miguelgrinberg-microblog megablanc$ virtualenv flask
New python executable in flask/bin/python
Installing setuptools, pip, wheel...
  Complete output from command /Users/megablanc/Dev...log/flask/bin/python -c "import sys, pip; sys...d\"] + sys.argv[1:]))" setuptools pip wheel:
  Can not perform a '--user' install. User site-packages are not visible in this virtualenv.
----------------------------------------
...Installing setuptools, pip, wheel...done.
Traceback (most recent call last):
  File "/Users/megablanc/Library/Python/2.7/bin/virtualenv", line 11, in <module>
    sys.exit(main())
  File "/Users/megablanc/Library/Python/2.7/lib/python/site-packages/virtualenv.py", line 832, in main
    symlink=options.symlink)
  File "/Users/megablanc/Library/Python/2.7/lib/python/site-packages/virtualenv.py", line 1004, in create_environment
    install_wheel(to_install, py_executable, search_dirs)
  File "/Users/megablanc/Library/Python/2.7/lib/python/site-packages/virtualenv.py", line 969, in install_wheel
    'PIP_NO_INDEX': '1'
  File "/Users/megablanc/Library/Python/2.7/lib/python/site-packages/virtualenv.py", line 910, in call_subprocess
    % (cmd_desc, proc.returncode))
OSError: Command /Users/megablanc/Dev...log/flask/bin/python -c "import sys, pip; sys...d\"] + sys.argv[1:]))" setuptools pip wheel failed with error code 1

You don't need to set the --user flag.您不需要设置--user标志。 After you create your virtualenv ( virtualenv flask ), activate it: source flask/bin/activate .创建 virtualenv ( virtualenv flask ) 后,激活它: source flask/bin/activate Your shell should look something like (flask) ~> .你的外壳应该看起来像(flask) ~>

Once your virtualenv is activated, you should be able to pip install packages without issue.一旦你的 virtualenv 被激活,你应该能够毫无问题地 pip 安装包。 For example, pip install numpy .例如, pip install numpy They'll be installed in: lib/python2.6/site-packages/ (for whatever version of Python you are using)它们将安装在: lib/python2.6/site-packages/ (适用于您使用的任何 Python 版本)

对我有用的是将$VIRTUAL_ENV_DIRECTORY/pyvenv.cfg更改为include-system-site-packages = true似乎很hacky。

There is a pip.conf file in ~/.pip. ~/.pip. There I changed the flag user=true to user=false using the command gedit pip.conf , after which I am able to create virtual environment successfully.在那里,我使用命令gedit pip.conf将标志user=true更改为user=false ,之后我能够成功创建虚拟环境。

In my case, there was a file in /etc/pip.conf setting the user=true secretly.就我而言, /etc/pip.conf中有一个文件秘密设置了user=true So, every time I activated a virtualenv, that config still affected the virtualenv.因此,每次我激活 virtualenv 时,该配置仍然会影响 virtualenv。

Removing that line worked for me.删除那条线对我有用。

当您的virutalenv myenv被激活(源 myenv/bin/activate)时,删除--user

Some people suggest you to edit /etc/pip.conf , which (i) requires superuser privileges and (ii) may break your whole system if done wrong.有些人建议您编辑/etc/pip.conf ,这 (i) 需要超级用户权限,并且 (ii) 如果操作不当可能会破坏整个系统。 So, it is better to keep it to only your user.因此,最好仅将其保留给您的用户。

Open up $HOME/.pip/pip.conf with a text editor.使用文本编辑器打开$HOME/.pip/pip.conf If it does not exist (which is the case in my Manjaro machine), create it.如果它不存在(在我的 Manjaro 机器中就是这种情况),请创建它。 Then add the lines below, save and close.然后添加下面的行,保存并关闭。

[global]
user=false

When you change a setting for your programs, prefer under $HOME for doing that if possible, which will (i) persist the setting among updates and reinstalling system (if you have separated your disk to / and /home partitions, of course) and (ii) will not possibly break further upgrades of the program, in this case, pip .当您更改程序的设置时,如果可能的话,最好在$HOME下执行此操作,这将 (i) 在更新和重新安装系统之间保留设置(当然,如果您已将磁盘分隔到//home分区)和(ii) 不会破坏程序的进一步升级,在这种情况下是pip

In my case I was doing a make test for the python disco mapreduce library.就我而言,我正在为 python disco mapreduce 库make test

So I modified the Makefile test section and removed the --user flag.所以我修改了Makefile测试部分并删除了--user标志。

In my case it was a custom python installation from anaconda was interfering with the system installation.就我而言,这是来自 anaconda 的自定义 python 安装干扰了系统安装。 Check which pip ... the solution is to either remove or move the custom installation of python.检查which pip ...解决方案是删除或移动 python 的自定义安装。

This worked for me, I only changed the $VIRTUAL_ENV_DIRECTORY/pyvenv.cfg to这对我有用,我只将$VIRTUAL_ENV_DIRECTORY/pyvenv.cfg

include-system-site-packages = true

The default setting默认设置

include-system-site-packages = false

Note:笔记:

> python --version 
Python 3.8.3 r c 1

Although the question was asked quite a pretty while ago, maybe another answer would be useful to someone anyway.尽管这个问题是很久以前提出的,但也许另一个答案对某人还是有用的。

If the described issue happens only when trying to install packages to virtualenv which is outside your home directory, the problem might be that user account you logged in does not have permissions on the folder you are trying to install to.如果仅在尝试将软件包安装到您的主目录之外的virtualenv 时发生所描述的问题,则问题可能是您登录的用户帐户对您尝试安装的文件夹没有权限。

Use chown to change / raise user's permission on the folder where target virtual environment is placed, for example:使用chown更改/提高用户对目标虚拟环境所在文件夹的权限,例如:

chown username /var/www/your-webproject-folder/ -r

or或者

sudo chown username /var/www/your-webproject-folder/ -r

if you have to do it with root.如果你必须用root来做。

Check if your the PIP_USER environment variable is set.检查是否设置了PIP_USER环境变量。
That fixed the issue for me.这为我解决了这个问题。
If you are using GitPod then this is a known issue .如果您使用的是GitPod,那么这是一个已知问题

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

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