Virtualenv: No module named '_socket' , No module named 'urllib3' | windows 7

Created on 19 Jul 2017  路  13Comments  路  Source: pypa/virtualenv

I have Windows 7 & Python 3.6.2 (v3.6.2:5fd33b5, Jul 8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32

#### When I run pip on VirtualEnv , here's what I get:

$ pip
Traceback (most recent call last):
  File "c:\users\user\python\work\test\lib\site-packages\pip\_vendor\requests\packages\__init__.py", line 27, in <module>
    from . import urllib3
  File "c:\users\user\python\work\test\lib\site-packages\pip\_vendor\requests\packages\urllib3\__init__.py", line 8, in <module>
    from .connectionpool import (
  File "c:\users\user\python\work\test\lib\site-packages\pip\_vendor\requests\packages\urllib3\connectionpool.py", line 7, in <module>
    from socket import error as SocketError, timeout as SocketTimeout
  File "C:\Python36\lib\socket.py", line 49, in <module>
    import _socket
ModuleNotFoundError: No module named '_socket'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python36\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Python36\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\user\python\work\test\Scripts\pip.exe\__main__.py", line 5, in <module>
  File "c:\users\user\python\work\test\lib\site-packages\pip\__init__.py", line 21, in <module>
    from pip._vendor.requests.packages.urllib3.exceptions import DependencyWarning
  File "c:\users\user\python\work\test\lib\site-packages\pip\_vendor\requests\__init__.py", line 62, in <module>
    from .packages.urllib3.exceptions import DependencyWarning
  File "c:\users\user\python\work\test\lib\site-packages\pip\_vendor\requests\packages\__init__.py", line 29, in <module>
    import urllib3
ModuleNotFoundError: No module named 'urllib3'

But globally pip work 馃憤

Most helpful comment

You shouldn't need to copy the DLLs into the virtualenv - they are normally referenced from their original location. This works for me (Python 3.6.1 64-bit on Windows 10).

>virtualenv pyt
Using base prefix 'c:\\users\\XXX\\appdata\\local\\programs\\python\\python36'
New python executable in C:\Work\Scratch\pyt\Scripts\python.exe
Installing setuptools, pip, wheel...done.
>.\pyt\Scripts\python.exe
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> import _socket
>>> _socket.__file__
'c:\\users\\XXX\\appdata\\local\\programs\\python\\python36\\DLLs\\_socket.pyd'
>>> import sys
>>> sys.path
['', 'C:\\Work\\Scratch\\pyt\\Scripts\\python36.zip', 'C:\\Work\\Scratch\\pyt\\DLLs', 'C:\\Work\\Scratch\\pyt\\lib', 'C:\\Work\\Scratch\\pyt\\Scripts', 'c:\\users\\XXX\\appdata\\local\\programs\\python\\python36\\Lib', 'c:\\users\\XXX\\appdata\\local\\programs\\python\\python36\\DLLs', 'C:\\Work\\Scratch\\pyt', 'C:\\Work\\Scratch\\pyt\\lib\\site-packages']
>>>

Looks like something is messing up your sys.path. Can you confirm what sys.path contains in your environment, and also check if you have any Python-related environment variables like $env:PYTHONPATH set?

All 13 comments

Looks to me like the latter is a bogus error caused by first one. If you try running python inside the virtualenv and import socket manually, this reproduces, right?

I'm running into this too. Yes, \path-to-env\Scripts\python.exe -c "import socket" reproduces the same error.

Right. So technically this bug is about virtualenv breaking the importing of C part of socket module, everything else is a side-effect

I'd say so. It looks like the Python36\DLLs directory is where _socket.pyd and other DLLs live, and none of them are getting copied into the virtual env.

You shouldn't need to copy the DLLs into the virtualenv - they are normally referenced from their original location. This works for me (Python 3.6.1 64-bit on Windows 10).

>virtualenv pyt
Using base prefix 'c:\\users\\XXX\\appdata\\local\\programs\\python\\python36'
New python executable in C:\Work\Scratch\pyt\Scripts\python.exe
Installing setuptools, pip, wheel...done.
>.\pyt\Scripts\python.exe
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> import _socket
>>> _socket.__file__
'c:\\users\\XXX\\appdata\\local\\programs\\python\\python36\\DLLs\\_socket.pyd'
>>> import sys
>>> sys.path
['', 'C:\\Work\\Scratch\\pyt\\Scripts\\python36.zip', 'C:\\Work\\Scratch\\pyt\\DLLs', 'C:\\Work\\Scratch\\pyt\\lib', 'C:\\Work\\Scratch\\pyt\\Scripts', 'c:\\users\\XXX\\appdata\\local\\programs\\python\\python36\\Lib', 'c:\\users\\XXX\\appdata\\local\\programs\\python\\python36\\DLLs', 'C:\\Work\\Scratch\\pyt', 'C:\\Work\\Scratch\\pyt\\lib\\site-packages']
>>>

Looks like something is messing up your sys.path. Can you confirm what sys.path contains in your environment, and also check if you have any Python-related environment variables like $env:PYTHONPATH set?

Just noticing your prompt ($) - are you doing this from a non-standard shell such as Git Bash? That may well be affecting your results. If that is the case, can you reproduce the issue in a standard Windows shell (cmd or powershell)?

The original filer was using some other shell, I'm using the regular Windows shell.

sys.path was pointing to the system python first, which it was indeed picking up from a PYTHONPATH in the env. I cleared that, rebuilt the env, and now "import socket" and pip seem to be much happier.

I appreciate the help.

Here again same error but with Windows bulit in shell cmd

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\user\python\4github>virtualenv test
PYTHONHOME is set.  You *must* activate the virtualenv before using it
Using base prefix 'C:\\Python36'
New python executable in C:\Users\user\python\4github\test\Scripts\python.exe
Installing setuptools, pip, wheel...done.

C:\Users\user\python\4github>.\Scripts\activate
The system cannot find the path specified.

C:\Users\user\python\4github>cd test

C:\Users\user\python\4github\test>.\Scripts\activate

(test) C:\Users\user\python\4github\test>pip
Traceback (most recent call last):
  File "c:\users\user\python\4github\test\lib\site-packages\pip\_vendor\requests
\packages\__init__.py", line 27, in <module>
    from . import urllib3
  File "c:\users\user\python\4github\test\lib\site-packages\pip\_vendor\requests
\packages\urllib3\__init__.py", line 8, in <module>
    from .connectionpool import (
  File "c:\users\user\python\4github\test\lib\site-packages\pip\_vendor\requests
\packages\urllib3\connectionpool.py", line 7, in <module>
    from socket import error as SocketError, timeout as SocketTimeout
  File "C:\Python36\lib\socket.py", line 49, in <module>
    import _socket
ModuleNotFoundError: No module named '_socket'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python36\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Python36\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\user\python\4github\test\Scripts\pip.exe\__main__.py", line 5,
in <module>
  File "c:\users\user\python\4github\test\lib\site-packages\pip\__init__.py", li
ne 21, in <module>
    from pip._vendor.requests.packages.urllib3.exceptions import DependencyWarni
ng
  File "c:\users\user\python\4github\test\lib\site-packages\pip\_vendor\requests
\__init__.py", line 62, in <module>
    from .packages.urllib3.exceptions import DependencyWarning
  File "c:\users\user\python\4github\test\lib\site-packages\pip\_vendor\requests
\packages\__init__.py", line 29, in <module>
    import urllib3
ModuleNotFoundError: No module named 'urllib3'

(test) C:\Users\user\python\4github\test>virtualenv pyt
Using base prefix 'c:\\python36'
Traceback (most recent call last):
  File "C:\Python36\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Python36\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Python36\Scripts\virtualenv.exe\__main__.py", line 9, in <module>
  File "c:\python36\lib\site-packages\virtualenv.py", line 713, in main
    symlink=options.symlink)
  File "c:\python36\lib\site-packages\virtualenv.py", line 925, in create_enviro
nment
    site_packages=site_packages, clear=clear, symlink=symlink))
  File "c:\python36\lib\site-packages\virtualenv.py", line 1132, in install_pyth
on
    copy_required_modules(home_dir, symlink)
  File "c:\python36\lib\site-packages\virtualenv.py", line 1062, in copy_require
d_modules
    dst_filename = change_prefix(filename, dst_prefix)
  File "c:\python36\lib\site-packages\virtualenv.py", line 1036, in change_prefi
x
    (filename, prefixes)
AssertionError: Filename C:\Python36\lib\os.py does not start with any of these
prefixes: ['C:\\python36', 'C:\\python36']

(test) C:\Users\user\python\4github\test>

Any way i did find cheap solution by creating the ENV in other PC _NEWER ONE_ then copy the files to my PC which is about 4 years old.

In that case every thing works

I encountered the first error message after installing into $HOME/.local from source on Linux and solved it by creating a symlink to $HOME/.local/lib64/python3.6/lib-dynload/ in $HOME/.local/lib64/python3.6/.

Thanks!

Here again same error but with Windows bulit in shell cmd

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\user\python\4github>virtualenv test
PYTHONHOME is set.  You *must* activate the virtualenv before using it
Using base prefix 'C:\\Python36'
New python executable in C:\Users\user\python\4github\test\Scripts\python.exe
Installing setuptools, pip, wheel...done.

C:\Users\user\python\4github>.\Scripts\activate
The system cannot find the path specified.

C:\Users\user\python\4github>cd test

C:\Users\user\python\4github\test>.\Scripts\activate

(test) C:\Users\user\python\4github\test>pip
Traceback (most recent call last):
  File "c:\users\user\python\4github\test\lib\site-packages\pip\_vendor\requests
\packages\__init__.py", line 27, in <module>
    from . import urllib3
  File "c:\users\user\python\4github\test\lib\site-packages\pip\_vendor\requests
\packages\urllib3\__init__.py", line 8, in <module>
    from .connectionpool import (
  File "c:\users\user\python\4github\test\lib\site-packages\pip\_vendor\requests
\packages\urllib3\connectionpool.py", line 7, in <module>
    from socket import error as SocketError, timeout as SocketTimeout
  File "C:\Python36\lib\socket.py", line 49, in <module>
    import _socket
ModuleNotFoundError: No module named '_socket'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python36\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Python36\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\user\python\4github\test\Scripts\pip.exe\__main__.py", line 5,
in <module>
  File "c:\users\user\python\4github\test\lib\site-packages\pip\__init__.py", li
ne 21, in <module>
    from pip._vendor.requests.packages.urllib3.exceptions import DependencyWarni
ng
  File "c:\users\user\python\4github\test\lib\site-packages\pip\_vendor\requests
\__init__.py", line 62, in <module>
    from .packages.urllib3.exceptions import DependencyWarning
  File "c:\users\user\python\4github\test\lib\site-packages\pip\_vendor\requests
\packages\__init__.py", line 29, in <module>
    import urllib3
ModuleNotFoundError: No module named 'urllib3'

(test) C:\Users\user\python\4github\test>virtualenv pyt
Using base prefix 'c:\\python36'
Traceback (most recent call last):
  File "C:\Python36\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Python36\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Python36\Scripts\virtualenv.exe\__main__.py", line 9, in <module>
  File "c:\python36\lib\site-packages\virtualenv.py", line 713, in main
    symlink=options.symlink)
  File "c:\python36\lib\site-packages\virtualenv.py", line 925, in create_enviro
nment
    site_packages=site_packages, clear=clear, symlink=symlink))
  File "c:\python36\lib\site-packages\virtualenv.py", line 1132, in install_pyth
on
    copy_required_modules(home_dir, symlink)
  File "c:\python36\lib\site-packages\virtualenv.py", line 1062, in copy_require
d_modules
    dst_filename = change_prefix(filename, dst_prefix)
  File "c:\python36\lib\site-packages\virtualenv.py", line 1036, in change_prefi
x
    (filename, prefixes)
AssertionError: Filename C:\Python36\lib\os.py does not start with any of these
prefixes: ['C:\\python36', 'C:\\python36']

(test) C:\Users\user\python\4github\test>

today I encountered same issue.
In the past I set PYTHONHOME variable in USER ENVIRONMENT, but it's redundant in the case of using python without virtualenv. When I created virtualenv it raised "ModuleNotFoundError: No module named '_socket'"

So solution for the whole situation is removing PYTHONHOME variable from USER ENVIRONMENT (or Global Environment) in Windows - System - Environment Variables

PYTHONHOME variable used with high priority when virtualenv activated. You can check it by printing sys.home.

Here again same error but with Windows bulit in shell cmd

Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

C:\Users\user\python\4github>virtualenv test
PYTHONHOME is set.  You *must* activate the virtualenv before using it
Using base prefix 'C:\\Python36'
New python executable in C:\Users\user\python\4github\test\Scripts\python.exe
Installing setuptools, pip, wheel...done.

C:\Users\user\python\4github>.\Scripts\activate
The system cannot find the path specified.

C:\Users\user\python\4github>cd test

C:\Users\user\python\4github\test>.\Scripts\activate

(test) C:\Users\user\python\4github\test>pip
Traceback (most recent call last):
  File "c:\users\user\python\4github\test\lib\site-packages\pip\_vendor\requests
\packages\__init__.py", line 27, in <module>
    from . import urllib3
  File "c:\users\user\python\4github\test\lib\site-packages\pip\_vendor\requests
\packages\urllib3\__init__.py", line 8, in <module>
    from .connectionpool import (
  File "c:\users\user\python\4github\test\lib\site-packages\pip\_vendor\requests
\packages\urllib3\connectionpool.py", line 7, in <module>
    from socket import error as SocketError, timeout as SocketTimeout
  File "C:\Python36\lib\socket.py", line 49, in <module>
    import _socket
ModuleNotFoundError: No module named '_socket'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python36\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Python36\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\user\python\4github\test\Scripts\pip.exe\__main__.py", line 5,
in <module>
  File "c:\users\user\python\4github\test\lib\site-packages\pip\__init__.py", li
ne 21, in <module>
    from pip._vendor.requests.packages.urllib3.exceptions import DependencyWarni
ng
  File "c:\users\user\python\4github\test\lib\site-packages\pip\_vendor\requests
\__init__.py", line 62, in <module>
    from .packages.urllib3.exceptions import DependencyWarning
  File "c:\users\user\python\4github\test\lib\site-packages\pip\_vendor\requests
\packages\__init__.py", line 29, in <module>
    import urllib3
ModuleNotFoundError: No module named 'urllib3'

(test) C:\Users\user\python\4github\test>virtualenv pyt
Using base prefix 'c:\\python36'
Traceback (most recent call last):
  File "C:\Python36\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\Python36\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Python36\Scripts\virtualenv.exe\__main__.py", line 9, in <module>
  File "c:\python36\lib\site-packages\virtualenv.py", line 713, in main
    symlink=options.symlink)
  File "c:\python36\lib\site-packages\virtualenv.py", line 925, in create_enviro
nment
    site_packages=site_packages, clear=clear, symlink=symlink))
  File "c:\python36\lib\site-packages\virtualenv.py", line 1132, in install_pyth
on
    copy_required_modules(home_dir, symlink)
  File "c:\python36\lib\site-packages\virtualenv.py", line 1062, in copy_require
d_modules
    dst_filename = change_prefix(filename, dst_prefix)
  File "c:\python36\lib\site-packages\virtualenv.py", line 1036, in change_prefi
x
    (filename, prefixes)
AssertionError: Filename C:\Python36\lib\os.py does not start with any of these
prefixes: ['C:\\python36', 'C:\\python36']

(test) C:\Users\user\python\4github\test>

today I encountered same issue.
In the past I set PYTHONHOME variable in USER ENVIRONMENT, but it's redundant in the case of using python without virtualenv. When I created virtualenv it raised "ModuleNotFoundError: No module named '_socket'"

So solution for the whole situation is removing PYTHONHOME variable from USER ENVIRONMENT (or Global Environment) in Windows - System - Environment Variables

PYTHONHOME variable used with high priority when virtualenv activated. You can check it by printing sys.home.

Adding to this I also deleted PYTHONPATH variable from USER ENVIRONMENT

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jwarren116 picture jwarren116  路  5Comments

erbatyr picture erbatyr  路  5Comments

earthgecko picture earthgecko  路  4Comments

asottile picture asottile  路  6Comments

Tset-Noitamotua picture Tset-Noitamotua  路  4Comments