-
-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
backports import path can not be overridden in Windows (Linux works fine) #75922
Comments
The Windows install of Python 2.7.13 defaults it's install into **C:\Python27**. It creates several subdirectories within this; one of which is **C:\Python27\lib\site-packages\backports**. So just out of the box (without this script installed): import backports
print(backports.__path__)
# C:\Python27\lib\site-packages\backports I have a custom package that sets the sys.path(0, "new path") entry which works great in Linux. I ship all my modules and settings in this path. This to work great in Windows too 'except' for the reference to Consider the block of code: import sys
from os import getcwd
from os.path import join
sys.path.insert(0, join(getcwd(), 'MyLibraries'))
# Now if we import backports, we should find the one in our MyLibraries directory
import backports
print(backports.__path__) # Nope... :(
# C:\Python27\lib\site-packages\backports
# What's weird is it's not entirely broken... you can do the following:
import chardet
print(chardet.__path__)
# Prints my path to MyLibraries/chardet
# So the path is correct and other libraries are coming in correctly What's really weird, is (in Windows) even if i delete the **C:\Python27\lib\site-packages\backports**, it still imports the module; so it's like part of the Python27 build in some way.
From what I can see the Microsoft version of Python27 can't seem to stop referencing (even when told not to) the _backports_ location. Linux works perfectly (as intended) but having customers port to this isn't an option for me. |
Where did you get your installer? I don't find any references to 'backports' in our repo, other than a couple of places in the docs where the word appears. I also don't understand the relationship between your MyPackages and a backports directory. They seem to be unrelated in your examples. |
Thank you for replying so quickly; the issue stems from a bug created here In my case, I had a lib\site-packages\backports in C:\Python27 that keeps To answer your other question: I got the installer from here: |
What is "the backports module"? I still don't understand what a backports directory, that I don't think our installer creates, has to do with your MyLibraries directory. I'm sorry that I'm having trouble following your explanation. Maybe you could list out the contents of your directories and what your sys.path is? |
The "backports" module you refer to as being in site-packages is not shipped with the standard library, it's a 3rd party addon that presumably you installed yourself. You'll need to let us know what you did to install it. You say you manipulate sys.path manually in your code. Please provide:
|
I'm not doing anything unusual. Just to recap, I installed Python27 (in Windows 7 in my case- but this problem happens in Windows 10 too). I did use import backports
print backports.__path__
# always prints: C:\Python27\Lib\site-packages\backports The problem arises because the following code won't work in my program: from import import ssl_match_hostname ^^ That throws an exception that the package doesn't exist. It's debugging it to the __path__ being incorrect that caused me to create this ticket. |
Sorry, I don't see any pip commands. Please could you include sample commands inline as text, and not as screenshots, as attached files aren't accessible from emails... As I say, I suspect the backports module is being imported before you change sys.path, and hence your sys.path change isn't affecting it. Sorry, but unless you can provide a precise set of steps to reproduce the issue, starting from the point where you install Python, I'm not sure what else we can do to help (but I will reiterate - this is *not* a problem with the Python install itself - we don't ship a site-packages/backports directory, that came with something you installed via pip). |
Ah, so backports is a package on pypi. You should report this problem wherever they do their bug tracking. It is *possible* there is some problem in 2.7 on windows, but it is much more likely to be a bug in backports on windows. In sort, python does not do *anything* special with a directory named backports; whatever is happening is a consequence of how that package (or one of the packages you have installed that installs into backports) interacts with the generic python import machinery. Perhaps Paul will see something of interest here, though. |
My feeling is still that it's an issue with things (i.e., imports) happening before you adjust sys.path. But without seeing actual code that reproduces the issue, there's no way of being sure. And if that is what's going on, it wouldn't be an issue with core Python. |
-- Download Instructions
-- Installation Instructions
** Heads up -- 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. -- Script Run # 1
Like you said... everything is fine; it's not a python issue... but hang on... Let's use pip and install some simple packages...
Let's run our script again (same one... same content). If you're doing this from the command line, then cut and paste:
So... what should you take from this?
Here is where it gets really weird...
Now lets run our Test.py again:
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:
Directory of C:\Python27\Lib\site-packages\backports 10/11/2017 06:30 PM <DIR> .
Directory of C:\Python.Test\Test\backports 10/11/2017 06:04 PM <DIR> .
I realize i'm frustrating you with my request, but hopefully this helps explain the problem better. |
Just to point out, i forgot the instructions of installing pip (right before) the following entry in my last post:
>> ** Heads up
>> So at this point, the C:\Python27\Lib\site-packages will contain the >>> following: pip, setuptools, and wheel. To be thorough, i should add that that i followed these instruction: https://pip.pypa.io/en/stable/installing/ Which required me to download get-pip.py from here https://bootstrap.pypa.io/get-pip.py and run it. The rest of the instructions (after the **heads up) comment are still all on note. |
Note that your instructions start with downloading the installer for 2.7.0, whereas the latest version of 2.7 is 2.7.14, which includes pip. backports is a strange beast of a package, which tries to emulate Python 3's namespace packages in Python 2. To do so, backports.__init__ must have a very specific incantation, which yours is lacking (it only contains "from __future__ import absolute_import"). See https://pypi.org/project/backports/ for more details. |
Also, one of the packages installed creates a file configparser-3.5.0-py2.7-nspkg.pth This has some very suspicious looking content: import sys, types, os;has_mfs = sys.version_info > (3, 5);p = os.path.join(sys._getframe(1).f_locals['sitedir'], *('backports',)); importlib = has_mfs and __import__('importlib.util');has_mfs and __import__('importlib.machinery');m = has_mfs and sys.modules.set default('backports', importlib.util.module_from_spec(importlib.machinery.PathFinder.find_spec('backports', [os.path.dirname(p)]))) ;m = m or sys.modules.setdefault('backports', types.ModuleType('backports'));mp = (m or []) and m.__dict__.setdefault('__path__',[ ]);(p not in mp) and mp.append(p) That's likely your problem. I've no idea what it's doing, but as Zachary says, the backports module is a strange beast... |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: