简体   繁体   中英

'module' object has no attribute 'compose'

Related to this question , but I'm on a newer Ubuntu and functools is installed but isn't working properly.

I'm on Ubuntu 16.04 64-bit, and whenever I try to do anything interesting in Python, it results in an error:

...something relevant to the actual application
...

 File "/usr/lib/python2.7/locale.py", line 17, in <module>
     import functools   File "/usr/local/lib/python2.7/dist-packages/functools.py", line 72, in
 <module>
     globals()['c_%s' % x] = globals()[x] = getattr(_functools, x) AttributeError: 'module' object has no attribute 'compose'

This has been tried with conjure-up , Robert and some actual python code. What can I do to fix it?

The relevant part of my functools.py looks like this:

try:
    import _functools
except ImportError:
    pass
else:
    for x in __all__:
        globals()['c_%s' % x] = globals()[x] = getattr(_functools, x)
    del x

So, as I understand it, python tries to import _functools , fails and tries to launch some loop with getattr and that's where it fails to find some module attribute.

I would consider a few things to get rolling again... First, when I run into issues like this, I usually make sure I can run the commands in the shell:

user@host:~$ python
>>> import functools
>>> # then do some other stuff to convince yourself it's working

Also, install virtualenv ( virtualenvwrapper ) and and then create one for testing:

user@host:~$ mkvirtualenv venv
(venv) user@host:~$ python
>>>> # test here

Another thing to try is fetch another version of Python, say, version 3 (for an OS like Ubuntu):

https://askubuntu.com/questions/865554/how-do-i-install-python-3-6-using-apt-get

Then create a virtenv with this version of Python:

user@host:~$ mkvirtualenv --python=/usr/bin/python3.6

It looks like your original installation of Python has some issues, and it might be hard to troubleshoot.

If this doesn't work, your primary Python might need to be repaired. If you are on Linux, having this problem can lead to other programs not functioning correctly.

If you think the installation of Python is broken or corrupt in some way, I might consider looking into reinstalling it, and there are several posts on SO that cover this.

I don't fully understand the context of the original question, but I wanted to point out that the compose function is in a different module: functional , not functools . This can be easy to miss if skimming through the Python 3.0 or 3.1 functools documentation.

import functional

functional.compose(outer_fn, inner_fn)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM