Module attribute mysteriously appears - python

$ python
Python 3.8.10 (default, Nov 14 2022, 12:59:47)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> urllib.request
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'urllib' has no attribute 'request'
>>> urllib.request
<module 'urllib.request' from '/usr/lib/python3.8/urllib/request.py'>
Why is urllib.request recognized as an attribute the second time I try to access it?

Related

Eventlet breaks select.poll

Eventlet monkey patch seems breaking py3 select.poll() on my ENV (i try to install openstack ironic), But openstack group could not reproduce this issue, anyone knows why?
I can simply reproduce it by:
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import eventlet
>>>
>>> eventlet.monkey_patch()
>>> import select
>>> select.poll
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'select' has no attribute 'poll'
>>> eventlet.version_info
(0, 25, 0)
>>>
It's intentional, as select.poll() is not "green".
See also: https://github.com/eventlet/eventlet/issues/608#issuecomment-612359458

Python module import fails when in file but from interpreter works

I have a module that I am unable to import from the import statement in the top of a file, but the same import works fine in the same interpreter.
Any thoughts on this?
➜ ~ python /home/barrett/Git/bnr_robot_software/src/bnr_robot_cloud /bnr_robot_cloud_common/src/bnr_robot_cloud_common/my_unique_script_name.py
Traceback (most recent call last):
File "/home/barrett/Git/bnr_robot_software/src/bnr_robot_cloud/bnr_robot_cloud_common/src/bnr_robot_cloud_common/my_unique_script_name.py", line 1, in <module>
from azure.servicebus import ServiceBusService
ImportError: No module named servicebus
➜ ~ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from azure.servicebus import ServiceBusService
>>>

'module' object has no attribute 'getusersitepackages'

I am going through the python tutorial and seem to be missing something very basic. The tutorial has:
>>> import site
>>> site.getusersitepackages()
'/home/user/.local/lib/python3.2/site-packages'
But I get the following:
$ python
Python 2.6.8 (unknown, Jun 9 2012, 11:30:32)
[GCC 4.5.3] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import site
>>> site.getusersitepackages()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'getusersitepackages'
As it says above, I am running Python 2.6.8 on cygwin.
Is this installation error?
According to the docs, this feature/function was added in python2.7 -- You're still on python 2.6

How to solve AttributeError when importing igraph?

When I import the igraph package in my project, I get an AttributeError. This only happens in the project directory:
[12:34][~]$ python2
Python 2.7.1 (r271:86832, Apr 15 2011, 12:09:10)
[GCC 4.5.2 20110127 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import igraph
>>>
[12:34][~]$ cd projectdir/
[12:34][projectdir]$ python2
Python 2.7.1 (r271:86832, Apr 15 2011, 12:09:10)
[GCC 4.5.2 20110127 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import igraph
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/igraph/__init__.py", line 42, in <module>
import gzip
File "/usr/lib/python2.7/gzip.py", line 36, in <module>
class GzipFile(io.BufferedIOBase):
AttributeError: 'module' object has no attribute 'BufferedIOBase'
>>>
There is no file igraph.py in the project directory:
[12:34][projectdir]$ ls -alR | grep igraph | wc -l
0
And there are no circular imports.
How can I solve this error?
Most likely, there is a module io in ~/projectdir or one of the paths the project configures. The gzip module imported by igraph starts with
import io
and expect the built-in io module, not your project's one. Look for an io directory, or io.py or io.pyc. It can also help to scrutinize sys.path for any other directories (maybe outside of ~/projectdir) that might contain modules named io.

String formatting error

Using the code print('{0} is not'.format('That that is not')) in Python 3.1.1, I get the following error:
AttributeError: 'str' object has no attribute 'format'
when I delete the line Netbeans automatically inserted at the beginning:
from distutils.command.bdist_dumb import format
which itself causes an error of
ImportError: cannot import name format
What am I doing wrong here?
You must be running an older version of Python. This does work in Python 3.1.1+:
$ python3
Python 3.1.1+ (r311:74480, Nov 2 2009, 14:49:22)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> '{0} is not'.format('That that is not')
'That that is not is not'
You will, however, get this error in Python 2.5.4:
$ python2.5
Python 2.5.4 (r254:67916, Jan 20 2010, 21:44:03)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> '{0} is not'.format('That that is not')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'format'
This feature seems to have been backported to Python 2.6, so you won't get this error there. You must be running Python < 2.6.

Categories

Resources