Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
122 views
in Technique[技术] by (71.8m points)

python - virtualenv on windows 7 returns AssertionError

Having trouble with virtualenv on Windows 7.

I run:

virtualenv _testenv

It returns:

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

I have the following environment variables:

PYTHONHOME=C:Python27
PYTHONPATH=c:Python27;c:Python27Lib
PYTHONSTARTUP=C:UsersLarry.pythonrc
PATH=%PYTHONHOME%;%PYTHONHOME%Scripts;etc

Installed 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:Python27Lib Still looking for a solution, I found and removed AppData/Python*. Reinstalled Python and now have a different error:

C:xbz>virtualenv _t
PYTHONHOME is set.  You *must* activate the virtualenv before using it
Overwriting _tLibsite.py with new content
New python executable in _tScriptspython2.7.exe
Not overwriting existing python script _tScriptspython.exe (you must use _tScriptspython2.7.exe)
Overwriting _tLibdistutils\__init__.py with new content
Installing setuptools..............
  Complete output from command C:xbz\_tScriptspython2.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:Python27Liburllib2.py", line 94, in <module>
    import httplib
  File "C:Python27Libhttplib.py", line 71, in <module>
    import socket
  File "C:Python27Libsocket.py", line 47, in <module>
    import _socket
ImportError: No module named _socket
----------------------------------------
...Installing setuptools...done.
Traceback (most recent call last):
  File "C:Python27Scriptsvirtualenv-script.py", line 9, in <module>
    load_entry_point('virtualenv==1.5.2', 'console_scripts', 'virtualenv')()
  File "C:Python27libsite-packagesvirtualenv.py", line 558, in main
    prompt=options.prompt)
  File "C:Python27libsite-packagesvirtualenv.py", line 654, in create_environment
    install_setuptools(py_executable, unzip=unzip_setuptools)
  File "C:Python27libsite-packagesvirtualenv.py", line 384, in install_setuptools
    _install_req(py_executable, unzip)
  File "C:Python27libsite-packagesvirtualenv.py", line 360, in _install_req
    cwd=cwd)
  File "C:Python27libsite-packagesvirtualenv.py", line 624, in call_subprocess
    % (cmd_desc, proc.returncode))
OSError: Command C:xbz\_tScriptspython2.7.exe -c "#!python
"""Bootstrap setuptoo...




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

I hacked Lib/socket.py and inserted:

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! 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:

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

Above

import _socket
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

3 year old question, but hopefully this answer can still help someone. 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. In Windows, you have to path out to python.exe, but it seems that in Linux/OS X you just path to the folder. Examples:

Windows:

virtualenv -p <PATH TO PYTHON.EXE> venv

Linux/Mac:

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

Both create a virtual environment in subfolder "venv" in current directory.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...