[issue31741] backports import path can not be overridden in Windows (Linux works fine)

Chris Caron report at bugs.python.org
Wed Oct 11 18:43:59 EDT 2017


Chris Caron <lead2gold at gmail.com> added the comment:

-- Download Instructions
* 64 Bit ver of Python v2.7 for Windows https://www.python.org/ftp/python/2.7/python-2.7.amd64.msi
* Microsoft Visual C++ Compiler for Python 2.7: https://www.microsoft.com/en-ca/download/details.aspx?id=44266. 

-- Installation Instructions
* Install Python v2.7 (run python-2.7.amd64.msi); choose default options. 
Note: In my case, i uninstalled my already installed version (for the purpose of this post) first and made sure to delete the C:\Python27 directory left behind). Then I reinstalled the package.
   Default Options:
      - Install for all users
      - Install location: C:\Python27\
      - All packages installed (5 of 5 subfeatures)
	  
** Heads up
So at this point, the C:\Python27\Lib\site-packages will contain the following: pip, setuptools,  and wheel.

-- A Starting Point
* Download the Python.Test.zip file i attached, but since you made it clear you want everything to be present in this message, i'll do my best to try to document it and paste content here: 
It's main contents is just to provide an alternative include directory called Test. In this test directory i provide six.py, odereddict, chardet, backports, socks and sockhandler.
>>>>> dir listing of Test
10/11/2017  06:04 PM    <DIR>          .
10/11/2017  06:04 PM    <DIR>          ..
10/11/2017  06:04 PM    <DIR>          backports
10/11/2017  06:04 PM    <DIR>          chardet
10/11/2017  06:04 PM             4,221 ordereddict.py
10/11/2017  06:04 PM             4,916 ordereddict.pyc
10/11/2017  06:04 PM            23,462 six.py
10/11/2017  06:04 PM            23,754 six.pyc
10/11/2017  06:04 PM            32,006 socks.py
10/11/2017  06:04 PM             2,913 sockshandler.py
10/11/2017  06:04 PM                 0 __init__.py
               7 File(s)         91,272 bytes
>>>>> End DIR Listing

Now lets introduce the second part of the zip file i provided. Test.py.  It's sitting next to (not in) the Test directory i listed above. It looks like this
>>>>> Test.py
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import sys
from os.path import join
from os.path import abspath
from os.path import dirname

print('Path Before Change: {0}'.format('\n'.join(sys.path)))
sys.path.insert(0, join(dirname(abspath(__file__)), 'Test'))
print('Path After Change: {0}'.format('\n'.join(sys.path)))

import backports
print('Backports Path: {0}'.format(backports.__path__))

import chardet
print('chardet Path: {0}'.format(chardet.__path__))

import six
print('six Path: {0}'.format(six.__file__))
>>>>> end Test.py

Those who already downloaded Test.py will notice i stripped out all of the comments.  Bear with me here, the actual code lines are still all unchanged.
I also placed the attached zip file into my root C:\ directory.  Not ideal for obvious reasons, but for this test, it makes the output small and easy to follow.

-- Script Run # 1
When i run the above script... right now... i get:
>>>>>
C:\Python.Test>python Test.py
Path Before Change: C:\Python.Test
C:\windows\system32\python27.zip
C:\Python27\DLLs
C:\Python27\lib
C:\Python27\lib\plat-win
C:\Python27\lib\lib-tk
C:\Python27
C:\Python27\lib\site-packages
Path Before Change: C:\Python.Test\Test
C:\Python.Test
C:\windows\system32\python27.zip
C:\Python27\DLLs
C:\Python27\lib
C:\Python27\lib\plat-win
C:\Python27\lib\lib-tk
C:\Python27
C:\Python27\lib\site-packages
Backports Path: ['C:\\Python.Test\\Test\\backports']
chardet Path: ['C:\\Python.Test\\Test\\chardet']
six Path: C:\Python.Test\Test\six.pyc
>>>>>

Like you said... everything is fine; it's not a python issue... but hang on...

Let's use pip and install some simple packages...
>>>>>
C:\Python.Test>pip install pylint
... lots of content flies by; but it's successful
>>>>>

Let's run our script again (same one... same content). If you're doing this from the command line, then cut and paste:
>>>>>
C:\Python.Test>python Test.py
Path Before Change: C:\Python.Test
C:\windows\system32\python27.zip
C:\Python27\DLLs
C:\Python27\lib
C:\Python27\lib\plat-win
C:\Python27\lib\lib-tk
C:\Python27
C:\Python27\lib\site-packages
Path Before Change: C:\Python.Test\Test
C:\Python.Test
C:\windows\system32\python27.zip
C:\Python27\DLLs
C:\Python27\lib
C:\Python27\lib\plat-win
C:\Python27\lib\lib-tk
C:\Python27
C:\Python27\lib\site-packages
Backports Path: ['C:\\Python27\\lib\\site-packages\\backports']
chardet Path: ['C:\\Python.Test\\Test\\chardet']
six Path: C:\Python.Test\Test\six.pyc
>>>>>

So... what should you take from this?
- backports is no longer referencing the one the PYTHONPATH (well sys.path.insert()) suggested otherwise.
- but we know it works because the other entries were loaded okay. We can see this from the 'six' and 'chardet' inport paths.

Here is where it gets really weird...
Lets install chardet:
>>>>>
C:\Python.Test>pip install chardet
... lots of content flies by; but it's successful
>>>>>

Now lets run our Test.py again:
>>>>>
C:\Python.Test>python Test.py
Path Before Change: C:\Python.Test
C:\windows\system32\python27.zip
C:\Python27\DLLs
C:\Python27\lib
C:\Python27\lib\plat-win
C:\Python27\lib\lib-tk
C:\Python27
C:\Python27\lib\site-packages
Path Before Change: C:\Python.Test\Test
C:\Python.Test
C:\windows\system32\python27.zip
C:\Python27\DLLs
C:\Python27\lib
C:\Python27\lib\plat-win
C:\Python27\lib\lib-tk
C:\Python27
C:\Python27\lib\site-packages
Backports Path: ['C:\\Python27\\lib\\site-packages\\backports']
chardet Path: ['C:\\Python.Test\\Test\\chardet']
six Path: C:\Python.Test\Test\six.pyc
>>>>>

The 'chardet' is getting imported correctly from the directory i want it to even though it's also available in the site-packages directory where the backports one is being picked up.

Here is the directory listings for you:
>>>>>
C:\Python.Test>dir C:\Python27\Lib\site-packages\backports\
 Volume in drive C has no label.
 Volume Serial Number is ACA9-DD63

 Directory of C:\Python27\Lib\site-packages\backports

10/11/2017  06:30 PM    <DIR>          .
10/11/2017  06:30 PM    <DIR>          ..
10/11/2017  06:30 PM    <DIR>          configparser
10/11/2017  06:30 PM             7,317 functools_lru_cache.py
10/11/2017  06:30 PM             7,378 functools_lru_cache.pyc
10/11/2017  06:30 PM                65 __init__.py
>>>>>

>>>>>
C:\Python.Test>dir Test\backports
 Volume in drive C has no label.
 Volume Serial Number is ACA9-DD63

 Directory of C:\Python.Test\Test\backports

10/11/2017  06:04 PM    <DIR>          .
10/11/2017  06:04 PM    <DIR>          ..
10/11/2017  06:04 PM    <DIR>          ssl_match_hostname
10/11/2017  06:04 PM                38 __init__.py
10/11/2017  06:04 PM               227 __init__.pyc
               2 File(s)            265 bytes
>>>>>

I realize i'm frustrating you with my request, but hopefully this helps explain the problem better.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue31741>
_______________________________________


More information about the Python-bugs-list mailing list