Cannot import pkg_resources

Oct 18, 2013 at 2:55 PM
Hi all,

I am generally a C++ guy, and very new to python and ... PTVS. I installed the integrated PTVS with VS2013 shell. I tried to get a simulation script running (imusim), however, I found that I cannot import pkg_resources, even though I run a script with just one line "import pkg_resources". A run time error is reported within pkg_resources, saying "cannot import name requires". But when I run the script in interactive mode (IPython), it just runs find. I am wondering what makes the behavior different.

I installed Python(x, y) 2.7.5.0, because the simulator depends on lots of packages included in it. The imusim library was written 2 years ago and no one is currently maintaining it. But it is a very good platform for simulating IMU sensor algorithms, so I want to get it running and probably help to maintain it. I don't think this is the cause of the problem though.

Can anyone please help me? Thanks a lot in advance!

Best,

Zell
Oct 18, 2013 at 3:01 PM
I also installed setuptools 1.1.6, which is also require by imusim. pkg_resources come from setuptools.
Coordinator
Oct 19, 2013 at 3:31 AM
It looks like it's this code in pkg_resources.py:
try:
    # Does the main program list any requirements?
    from __main__ import __requires__
except ImportError:
    pass # No: just use the default working set based on sys.path
else:
    # Yes: ensure the requirements are met, by prefixing sys.path if necessary
So if you just keep running past the error, it'll catch it and continue. Having said that, with default settings, we should not be reporting it to you in the first place. Can you please check the following settings:

Tools -> Options -> Debugging -> General -> Enable Just My Code (should be set by default)
Tools -> Options -> Python Tools -> Debugging -> Enable debugging of the Python standard library (should be cleared by default)
Debug -> Exceptions -> Python Exceptions -> Python 2.x -> exceptions.ImportError, (Thrown column should be cleared by default, User-Unhandled should be set by default)

If these are all at the default settings, then there is some bug on our side. I couldn't reproduce it with stock Python 2.7 + pkg_resources, so I'll download Python(x,y) and see if I can repro it there.

In the meantime, to avoid hitting this exception, you can clear both checkboxes for exceptions.ImportError (but then you won't get a report when one of your own imports fail and you don't have an except block to handle that).
Oct 21, 2013 at 7:07 PM
Thanks so much for your reply!

I check the settings:
Tools -> Options -> Debugging -> General -> Enable Just My Code: the first (visual basic) is clear, and the second (Managed only) is set.
Is this correct?

The other two settings are in their default settings. I actually didn't touch these settings after I installed the integrated PTVS.

I cleared both checkboxes for exceptions.ImportError, this avoids the import error, but another error is thown at line 2443 in pkg_resources.py (if self.key=='distribute':):

exceptions.ValueError occurred
Message: 'c:\python27\lib\site-packages\setuptools-1.1.6-py2.7.egg' is not in list

What list is the list mentioned here?

Thanks!

Zell
Oct 21, 2013 at 7:08 PM
Thanks so much for your reply!

I check the settings:
Tools -> Options -> Debugging -> General -> Enable Just My Code: the first (visual basic) is clear, and the second (Managed only) is set.
Is this correct?

The other two settings are in their default settings. I actually didn't touch these settings after I installed the integrated PTVS.

I cleared both checkboxes for exceptions.ImportError, this avoids the import error, but another error is thown at line 2443 in pkg_resources.py (if self.key=='distribute':):

exceptions.ValueError occurred
Message: 'c:\python27\lib\site-packages\setuptools-1.1.6-py2.7.egg' is not in list

What list is the list mentioned here?

Thanks!

Zell
Coordinator
Oct 22, 2013 at 4:14 AM
Edited Oct 22, 2013 at 4:15 AM
The message comes from the exception, PTVS just prints it out as is. It's actually this code:
try:
    np = npath.index(nloc, p+1)
except:
    break
So, again, this is something that's safe to ignore in the same manner.

The real problem here is that those exceptions are being reported to you even though they are clearly immediately handled, and are in library code, anyway.

You've mentioned that you cleared both checkboxes for exceptions.ImportError. Were they both set, or just the "Unhandled" one?
Oct 22, 2013 at 2:26 PM
Thanks for your reply! I only found one exceptions.ImportError in Debug->Exceptions->Python Exceptions->Python 2.x->exceptions->exceptions.ImportError, both Thrown and User-unhandled are cleared. But it still reports the same error as in my last post. Before, only User-unhandled was checked.
Oct 22, 2013 at 2:33 PM
In line 2443 in pkg_resources.py ( if self.key=='distribute': ) where the error occurs, the value of self.key is 'setuptools'.
Coordinator
Oct 23, 2013 at 7:00 PM
This new exception is a different one - exceptions.ValueError - so, to use the same workaround, you'll need to clear the checkbox for that one as well.

There have been several other reports about exceptions being improperly reported when they shouldn't be; we're investigating this issue.
Oct 23, 2013 at 8:53 PM
Thanks! It works now!

This is really a neat free product, I really like it. I have been using VS quite often, and I just feel so comfortable when using it to program. I appreciate all your effort in developing this cool product and taking care of our questions!
Nov 8, 2013 at 3:26 AM
Edited Nov 8, 2013 at 4:27 AM
Hi all,

I facing quite similar issue too. I received ValueError occurred message "'c:\python27\lib\site-packages\setuptools-1.3-py2.7.egg' is not in list" for line import pkg_resources. But I checked, setuptools-1.3-py2.7.egg is exist at that folder.

I have no idea why it will happen since before that, same code is able to run correctly. This error message popup after I reinstalled pyenchant (due to I accidentally delete 1 file used by pyenchant).

The program able to work again after I follow instruction here by cleared the checkbox for exception.ValueError, but I not sure it will have any side effect or not.

Thank you.
Nov 8, 2013 at 3:57 AM
May I know setuptools need to be in Solution Explorer -> 'Your' solution -> 'Your' project -> Python Environments -> Python 2.7 -> (setuptools need to be here??). I found my one do not have setuptools there.
Nov 8, 2013 at 5:13 AM
I think it is due to this issue Analyzer does not look inside .egg files.

If I extract the setuptools-1.3-py2.7.egg to become a folder instead of compress file, the problem will disappear.
Coordinator
Nov 8, 2013 at 6:38 AM
Hm... if that's the case, then it would be more specifically due to #1159, which is about exception handling not being able to determine the handled/unhandled status when code is in a .zip/.egg. That is a good point, thank you for bringing it up.