简体   繁体   English

Windows 7 上的 virtualenv 返回 AssertionError

[英]virtualenv on windows 7 returns AssertionError

Having trouble with virtualenv on Windows 7.在 Windows 7 上使用 virtualenv 时遇到问题。

I run:我跑:

virtualenv _testenv

It returns:它返回:

Traceback (most recent call last):
  File "C:\Python27\Scripts\virtualenv-script.py", line 9, in <module>
    load_entry_point('virtualenv==1.5.2', 'console_scripts', 'virtualenv')()
  File "C:\Python27\lib\site-packages\virtualenv.py", line 558, in main
    prompt=options.prompt)
  File "C:\Python27\lib\site-packages\virtualenv.py", line 647, in create_environment
    site_packages=site_packages, clear=clear))
  File "C:\Python27\lib\site-packages\virtualenv.py", line 771, in install_python
    copy_required_modules(home_dir)
  File "C:\Python27\lib\site-packages\virtualenv.py", line 725, in copy_required_modules
    dst_filename = change_prefix(filename, dst_prefix)
  File "C:\Python27\lib\site-packages\virtualenv.py", line 710, in change_prefix
    (filename, prefixes)
AssertionError: Filename c:\Python27\Lib\os.py does not start with any of these prefixes: ['C:\\Python27']

I have the following environment variables:我有以下环境变量:

PYTHONHOME=C:\Python27
PYTHONPATH=c:\Python27;c:\Python27\Lib
PYTHONSTARTUP=C:\Users\Larry\.pythonrc
PATH=%PYTHONHOME%\;%PYTHONHOME%\Scripts;etc

Installed ActiveState Python:已安装的 ActiveState Python:

ActivePython 2.7.2.5 (ActiveState Software Inc.) based on
Python 2.7.2 (default, Jun 24 2011, 12:21:10) [MSC v.1500 32 bit (Intel)] on win32

I updated the PYTHONPATH=C:\\Python27;C:\\Python27\\Lib Still looking for a solution, I found and removed AppData/Python*.我更新了 PYTHONPATH=C:\\Python27;C:\\Python27\\Lib 仍在寻找解决方案,我找到并删除了 AppData/Python*。 Reinstalled Python and now have a different error:重新安装 Python,现在有一个不同的错误:

C:\xbz>virtualenv _t
PYTHONHOME is set.  You *must* activate the virtualenv before using it
Overwriting _t\Lib\site.py with new content
New python executable in _t\Scripts\python2.7.exe
Not overwriting existing python script _t\Scripts\python.exe (you must use _t\Scripts\python2.7.exe)
Overwriting _t\Lib\distutils\__init__.py with new content
Installing setuptools..............
  Complete output from command C:\xbz\_t\Scripts\python2.7.exe -c "#!python
\"\"\"Bootstrap setuptoo...


" --always-copy -U setuptools:
  Traceback (most recent call last):
  File "<string>", line 278, in <module>
  File "<string>", line 210, in main
  File "<string>", line 132, in download_setuptools
  File "C:\Python27\Lib\urllib2.py", line 94, in <module>
    import httplib
  File "C:\Python27\Lib\httplib.py", line 71, in <module>
    import socket
  File "C:\Python27\Lib\socket.py", line 47, in <module>
    import _socket
ImportError: No module named _socket
----------------------------------------
...Installing setuptools...done.
Traceback (most recent call last):
  File "C:\Python27\Scripts\virtualenv-script.py", line 9, in <module>
    load_entry_point('virtualenv==1.5.2', 'console_scripts', 'virtualenv')()
  File "C:\Python27\lib\site-packages\virtualenv.py", line 558, in main
    prompt=options.prompt)
  File "C:\Python27\lib\site-packages\virtualenv.py", line 654, in create_environment
    install_setuptools(py_executable, unzip=unzip_setuptools)
  File "C:\Python27\lib\site-packages\virtualenv.py", line 384, in install_setuptools
    _install_req(py_executable, unzip)
  File "C:\Python27\lib\site-packages\virtualenv.py", line 360, in _install_req
    cwd=cwd)
  File "C:\Python27\lib\site-packages\virtualenv.py", line 624, in call_subprocess
    % (cmd_desc, proc.returncode))
OSError: Command C:\xbz\_t\Scripts\python2.7.exe -c "#!python
\"\"\"Bootstrap setuptoo...




" --always-copy -U setuptools failed with error code 1

I hacked Lib/socket.py and inserted:我破解了 Lib/socket.py 并插入:

import sys
sys.path = ['', 'C:\\Python27\\lib\\site-packages\\dotcloud-0.3.1-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\dotcloud.cli-0.3.1-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\flask-0.7dev_20110622-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\werkzeug-0.6.2-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\gunicorn-0.12.2-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\wtforms-0.6.3-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\repoze.browserid-0.3-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\paste-1.7.5.1-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\django_pjax-1.0-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\paramiko-1.7.7.1-py2.7.egg', 'C:\\Python27\\lib\\site-packages\\pycrypto-2.4.1-py2.7-win32.egg', 'C:\\Python27', 'C:\\Python27\\Lib', 'C:\\Windows\\system32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27\\lib\\site-packages', 'C:\\Python27\\lib\\site-packages\\PIL', 'C:\\Python27\\lib\\site-packages\\win32', 'C:\\Python27\\lib\\site-packages\\win32\\lib', 'C:\\Python27\\lib\\site-packages\\Pythonwin', 'C:\\Python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg-info', 'C:\\Python27\\Scripts', 'C:\\Python27\\Lib\\site-packages\\django\\bin']

Above以上

import _socket

The reason was that I was able to import socket from straight python prompt!原因是我能够从直接的 python 提示符导入套接字! So stuffed my existing path.所以塞满了我现有的路径。 I haven't narrowed down exactly which directory made it happy.我还没有确切地缩小哪个目录使它快乐。 It at least will reveal to someone else why I am getting the error without it.它至少会向其他人揭示为什么我会在没有它的情况下收到错误。

Ideas?想法? Suggestions?建议? Thank you.谢谢你。 :) :)


I hacked Lib/socket.py and inserted:我破解了 Lib/socket.py 并插入:

import sys
sys.path.append('C:\\Python27\\DLLs')

Above以上

import _socket

3 year old question, but hopefully this answer can still help someone. 3岁的问题,但希望这个答案仍然可以帮助某人。 Rather than setting the environment variables (which mysteriously didn't work for me), you can pass the path to your Python installation when setting up the virtual environment.您可以在设置虚拟环境时将路径传递到 Python 安装,而不是设置环境变量(这对我来说很奇怪)。 In Windows, you have to path out to python.exe, but it seems that in Linux/OS X you just path to the folder.在 Windows 中,您必须指向 python.exe,但在 Linux/OS X 中,您似乎只是指向该文件夹的路径。 Examples:例子:

Windows:视窗:

virtualenv -p <PATH TO PYTHON.EXE> venv

Linux/Mac: Linux/Mac:

virtualenv -p </user/path/to/python> venv

Both create a virtual environment in subfolder "venv" in current directory.两者都在当前目录的子文件夹“venv”中创建一个虚拟环境。

Try to set PYTHONPATH to PYTHONPATH=C:\\Python27;C:\\Python27\\Lib (uppercase C at the start).尝试将PYTHONPATH设置为PYTHONPATH=C:\\Python27;C:\\Python27\\Lib (开头的大写 C)。

This can be done at the command prompt by typing set PYTHONPATH=C:\\Python27;C:\\Python27\\Lib .这可以通过在命令提示符下输入set PYTHONPATH=C:\\Python27;C:\\Python27\\Lib

PYTHONPATH will revert back to whatever it previously was once that command prompt window is closed.一旦该命令提示符窗口关闭, PYTHONPATH将恢复到以前的状态。

There is similar problem currently that shows error:目前有类似的问题显示错误:

AssertionError: Filename C:\Python27\Lib\os.py does not start with any of 
these prefixes: ['C:\\python27']

The difference is in 'C:\\python27' being lower case.不同之处在于 'C:\\python27' 是小写的。 So the problem manifest itself in that you can not install new virtualenv or make a nested virtualenvs (we do it for testing sometimes).所以问题表现在你不能安装新的 virtualenv 或创建一个嵌套的 virtualenvs(我们有时这样做是为了测试)。 The cause is in the conent of the PYTHONPATH原因在于 PYTHONPATH 的内容

PYTHONPATH=C:\Python27;C:\Python27\Lib

For some reason sys.path in virtualenv.py will return c:\\python27, but path to required modules will come form the PYTHONPATH and start with 'C:\\Python27\\Lib', hence the assertion error.由于某种原因,virtualenv.py 中的 sys.path 将返回 c:\\python27,但所需模块的路径将来自 PYTHONPATH 并以“C:\\Python27\\Lib”开头,因此会出现断言错误。

Long story short, just unset the PYTHONPATH .长话短说,只需取消设置 PYTHONPATH

You can try the following (supppose your python is in global path):您可以尝试以下操作(假设您的 python 在全局路径中):

python -m virtualenv [foldername]

This works for me, Win 10, virtualenv 15.1.0这对我有用,Win 10,virtualenv 15.1.0

I get it from this video , it will excute the python lib instead of calling windows exe.我从这个视频中得到它,它会执行 python 库而不是调用 windows exe。

对于 _socket 错误,请将您的 pythonpython 路径更改为:

PYTHONPATH=C:\Python27;C:\Python27\Lib;C:\Python27\DLLs

I have added我已经添加了

if is_win:    
        prefixes.append('C:\PYTHON27')    

to virtualenv.py and it works.到 virtualenv.py 并且它有效。

Its strange but from the error message它很奇怪,但从错误消息

AssertionError: Filename c:\Python27\Lib\os.py does not start with any of these prefixes: ['C:\\Python27']

It seems, it expects the path-name for the file os.py to start with upper case 'C' and the prefix sanity check is case sensitive.看来,它期望文件os.py的路径名以大写的“C”开头,并且前缀完整性检查区分大小写。

As the path to the library is derived from PYTHONPATH and in your case the drive letter is in lower case, it seems logical to change it to upper case to resolve the issue.由于库的路径是从PYTHONPATH派生的,并且在您的情况下驱动器号为小写,将其更改为大写以解决问题似乎是合乎逻辑的。

like喜欢

PYTHONPATH=C:\Python27;C:\Python27\Lib

I hate "summary" answers, but as I just went through a very similar issue I thought I would post my solution here as well which draws from several of these answers.我讨厌“摘要”答案,但当我刚刚经历了一个非常相似的问题时,我想我也会在这里发布我的解决方案,它来自这些答案中的几个。

The assert error was caused because I did not have a PYTHONPATH environment variable setup.断言错误是因为我没有PYTHONPATH环境变量设置。

The socket error was caused because I did not include the PythonXX\\DLLs folder.造成套接字错误是因为我没有包含 PythonXX\\DLLs 文件夹。

The full PYTHONPATH environment variable should look follows:完整的 PYTHONPATH 环境变量应如下所示:

PYTHONPATH=C:\Python27;C:\Python27\Lib;C:\Python27\DLLs

This is an error already submitted to the Python development team: https://github.com/pypa/virtualenv/pull/697这是已经提交给Python开发团队的错误: https : //github.com/pypa/virtualenv/pull/697

In the meanwhile why not just change the Python installation folder name to (ptyhon27) to make the assertion work, or if you feel more confortable with that just reinstall python using the alternative location.同时,为什么不将 Python 安装文件夹名称更改为 (ptyhon27) 以使断言工作,或者如果您觉得更舒服,只需使用备用位置重新安装 python。 It works with no issue.它没有问题。

I also ran into this problem on Windows 7. My Python27 installation was under C:\\Program Files, which obviously contains a space in the path.我在 Windows 7 上也遇到了这个问题。我的 Python27 安装在 C:\\Program Files 下,显然路径中包含一个空格。 So, on a separate Windows 7 system that did not contain Python, I did a fresh install of Python27 under C:\\Python27 (the default installation path), followed by an install of setuptools (for easy_install).因此,在不包含 Python 的单独 Windows 7 系统上,我在 C:\\Python27(默认安装路径)下全新安装了 Python27,然后安装了 setuptools(用于 easy_install)。

Afterwards, I was able to install virtualenv CLEANLY without the above assertion error (I used easy_install).之后,我能够干净地安装 virtualenv 而没有上述断言错误(我使用了easy_install)。

I know that the OP's system is already using the default path, but I thought I would add my experience here as a possible solution for certain specific cases.我知道 OP 的系统已经在使用默认路径,但我想我会在这里添加我的经验作为某些特定情况的可能解决方案。

This issue is presumably a hangover from other more case-sensitive file systems.这个问题可能是其他更区分大小写的文件系统的后遗症。

Complete solution:完整的解决方案:

  1. Read the error message from virtualenv.阅读来自 virtualenv 的错误消息。 Remember the part where it says "does not start with any of these prefixes: ['C:\\\\Python27']".记住它说“不以这些前缀中的任何一个开头:['C:\\\\Python27']”的部分。

  2. Edit PYTHONPATH, or create it if you don't have one (Start+Break, Advanced system settings, Environment Variables).编辑 PYTHONPATH,或者在没有的情况下创建它(Start+Break、Advanced system settings、Environment Variables)。 It shouldn't matter if it's a user variable or a system variable, unless you plan to switch user accounts.除非您计划切换用户帐户,否则它是用户变量还是系统变量应该无关紧要。

  3. Make the case match the error message.使案例与错误消息匹配。 BOTH the drive letter AND the folder name must match (presumably intermediate folders as well, if you didn't install to C:\\Python27).驱动器号和文件夹名称都必须匹配(如果您没有安装到 C:\\Python27,大概也是中间文件夹)。 You can ignore the double backslash, one is fine.您可以忽略双反斜杠,一个就可以了。

The only change I made to fix the bug was as follows.我为修复该错误所做的唯一更改如下。 The change should take effect for any new command / terminal sessions (close your open cmd.exe / powershell / etc. windows).更改应该对任何新命令/终端会话生效(关闭打开的 cmd.exe / powershell / 等窗口)。

  • Old state: PYTHONPATH = C:\\PYTHON27;C:\\PYTHON27\\LIB;C:\\PYTHON27\\DLLS旧状态:PYTHONPATH = C:\\PYTHON27;C:\\PYTHON27\\LIB;C:\\PYTHON27\\DLLS

  • New state: PYTHONPATH = C:\\Python27;C:\\Python27\\LIB;C:\\Python27\\DLLS新状态:PYTHONPATH = C:\\Python27;C:\\Python27\\LIB;C:\\Python27\\DLLS

If you have any other items in your PYTHONPATH, you might as well change those too, but it probably won't affect virtualenv's ability to run.如果您的 PYTHONPATH 中有任何其他项目,您也可以更改它们,但它可能不会影响 virtualenv 的运行能力。

Change "virtualenv.py" --> change_prefix with:更改“virtualenv.py”--> change_prefix 为:

def change_prefix(filename, dst_prefix):
  ...
 prefixes = sorted(prefixes, key=len, reverse=True)
 filename = str(os.path.abspath(filename))[0].lower() + str(os.path.abspath(filename))[1:]
 for src_prefix in prefixes:
    if filename.startswith(src_prefix):
        _, relpath = filename.split(src_prefix, 1)
        if src_prefix != os.sep: # sys.prefix == "/"
            assert relpath[0] == os.sep
            relpath = relpath[1:]
        return join(dst_prefix, relpath)
 assert False, "Filename %s does not start with any of these prefixes: %s" % \
    (filename, prefixes)

  ...

I had the same assertion error from a slightly different cause.我有相同的断言错误,原因略有不同。 The error was does not start with any of these prefixes: ['C:\\\\python27'] and note the lowercase "p".错误does not start with any of these prefixes: ['C:\\\\python27']并注意小写的“p”。 The actual folder names all use capital-P Python27.实际文件夹名称均使用大写-P Python27。 All the prefixes in PTYHONPATH were correct. PTYHONPATH 中的所有前缀都是正确的。 However I had entered the PYTHONHOME variable as C:\\python27 and although this was fine for Python, it caused the error in virtualenv.然而,我已经将 PYTHONHOME 变量输入为C:\\python27 python27,虽然这对 Python 来说很好,但它导致了 virtualenv 中的错误。

Windows solution:视窗解决方案:

This is due to the difference between PYTHONPATH variable path and the one pipenv is expecting.这是由于PYTHONPATH变量路径和预期的pipenv之间的差异。

Suppose System Variable has below PYTHONPATH‍ ,假设系统变量低于PYTHONPATH‍

PYTHONPATH = C:\User\Bruce\AppData\Local\Programs\Python\Python37-32

And pipenv is looking for PYTHONPATH something like below: pipenv正在寻找PYTHONPATH ,如下所示:

PYTHONPATH = C:\users\bruce\appdata\local\programs\python\python37-32

Here, observe that the path text pipenv is looking has different case than what is set in System Environment variable.在这里,观察pipenv正在寻找的路径文本与系统环境变量中设置的大小写不同。

To solve this issue try below steps by opening command prompt in folder where pipenv is to run:要解决此问题,请尝试通过在运行pipenv文件夹中打开命令提示符来执行以下步骤:

> set PYTHONPATH=C:\users\bruce\appdata\local\programs\python\python37-32

You need to give exact same path that is shown in the AssertionError.您需要提供与 AssertionError 中显示的完全相同的路径。

Then run below command to create pipenv然后运行下面的命令来创建pipenv

> pipenv install numpy

Any other library can be installed可以安装任何其他库

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

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