ImportError: cannot import name wraps - python

I'm using python 2.7.6 on Ubuntu 14.04.2 LTS. I'm using mock to mock some unittests and noticing when I import mock it fails importing wraps.
Not sure if there's a different version of mock or six I should be using for it's import to work? Couldn't find any relevant answers and I'm not using virtual environments.
mock module says it's compatible with python 2.7.x: https://pypi.python.org/pypi/mock
mock==1.1.3
six==1.9.0
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from mock import Mock
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/mock/__init__.py", line 2, in <module>
import mock.mock as _mock
File "/usr/local/lib/python2.7/dist-packages/mock/mock.py", line 68, in <module>
from six import wraps
ImportError: cannot import name wraps
also tried with sudo with no luck.
$ sudo python -c 'from six import wraps'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: cannot import name wraps

Installed mock==1.0.1 and that worked for some reason. (shrugs)
edit: The real fix for me was to updated setuptools to the latest and it allowed me to upgrade mock and six to the latest. I was on setuptools 3.3. In my case I also had to remove said modules by hand because they were owned by OS in '/usr/local/lib/python2.7/dist-packages/'
check versions of everything
pip freeze | grep -e six -e mock
easy_install --version
Update everything
wget https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
pip install mock --upgrade
pip install six --upgrade
Thanks #lifeless

I encountered the same issue on my mac, which I was able to fix by realizing that my python's sys.path contained both
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/
and
/Library/Python/2.7/site-packages/
with the former earlier than the latter.
You can test if this is happening to you by running the following in the python console.
import six
six.__version__
my python was loading an outdated six.py from the former directory (which didn't have wrapper), even though pip had installed a newer version six in the second directory. (It seems mac's framework comes with a version of six by default.)
I was able to fix it by moving six.py and six.pyc out of the first directory (requires sudo access), so that python would find the newer version of six in the second directory. I'm sure you could also change the ordering of the paths in sys.path.
To find the older version of six that need to be deleted run this from the terminal console
find /System/Library/Frameworks/Python.framework/Versions -name six.py*

so mock 1.1.1 and above defines a versioned requirement on six 1.7 or above:
https://github.com/testing-cabal/mock/blob/master/requirements.txt#L6
This gets reflected into setuptools metadata by pbr, which there is a versioned setup_requires dependency on:
https://github.com/testing-cabal/mock/blob/master/setup.py#L17
So there are a couple of possibilities:
1) six 1.7 is not new enough
2) there's a distro six package claiming to be 1.9.0 that doesn't have wraps for some reason
3) the setuptools in use didn't integrate properly with pbr and deps are missing
4) the wheel metadata isn't being interrogated properly by your pip/setuptools combination.
We do have a hard requirement for setuptools 17.1, and that was only explicitly reported by setup.py more recently. I'd love it if you can figure which of these is the case and update https://github.com/testing-cabal/mock/issues/298 so that we can fix whatever interaction is leading to this silent failure of setup.py / wheels.

On Mac OSX, the previously installed version of six was blocking my upgraded version from being used. I verified this, as previously suggested by running the following in my interpreter:
import six
six.__version__
To fix this I moved the file:
mv/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six.py
/tmp/old_six.py
This is stated already in another answer on this site, but I wanted to provide a more streamlined response.

I originally had an issue with old "OS-owned" versions of and pip/setuptools. After I installed pip manually, like so:
wget https://bootstrap.pypa.io/get-pip.py
sudo python get-pip.py
sudo ln -s /usr/local/bin/pip /usr/bin/pip
And then installing the latest version of pip, mock and six, I still had the problem you've described above. Turns out that I had six installed twice in:
/usr/lib/python2.7/dist-packages/
and in
/usr/local/lib/python2.7/dist-packages/
After I removed the six from /usr/lib/ it worked fine:
rm /usr/lib/python2.7/dist-packages/*six*

I did a pip install of six==1.9.0 and it took the new version. It seems like mock==1.3.0 doesn't properly define the version of six that it needs to get wraps support.

Another solution is setting your PYTHONPATH environment variable to point to the installed packages.
Setting my environment variable in my bash config so that:
PYTHONPATH=/Library/Python/2.7/site-packages
Allowed me to run tests in terminal (without removing/renaming any libraries, etc).
However, when using PyCharm, it was less-than-helpfully not correctly importing this environment variable. Even though PyCharm was showing as including parent variables (with that listed in the ones it showed importing), it seems this import wasn't working correctly.
Manually setting the environment variable to the above in the PyCharm run configuration resolves this.
I am unsure if PyCharm overwrites the PYTHONPATH variable after importing it from system environment variables or some other trickery, but this did resolve the error for me.

Though you aren't using a virtual environment like virtualenv, it's certainly a great use case for it. By sandboxing your Python installation and all the dependencies for your project, you can avoid hacking away at the global/default python installation entirely, which is where a lot of the complexity/difficulty comes from.
This is what I used when I got the wraps error - requirements.txt contains mock==2.0.0 and six==1.10.0:
cd <my_project>
virtualenv venv
source venv/bin/activate
sudo pip install -r requirements.txt
Not only is this simpler to use in my opinion, it's also simpler to document for people who might want to run your code.

I found a interesting things!
There is a file named "functools.py" in my project root path, and while I run my project , pycharm will raise ImportError.
So I rename my file fix this problem~~

Related

How can i install python modules that have spaces in between?

I wanted to run a script that would scan my network and that script uses a awesome library called who-is-on-my-wifi. I have installed the module to run the script but i get errors from the prompt saying that it cannot detect such a module in the system.
This is the script.
from who_is_on_my_wifi import *
WHO = who()
for i in range(0, len(WHO)):
print(WHO[i])
And this is the error that i get.
python scanner.py
Traceback (most recent call last):
File "scanner.py", line 1, in <module>
from who_is_on_my_wifi import *
ImportError: No module named who_is_on_my_wifi
This is the proof that i have installed the module
pip3 install 'who_is_on_my_wifi'
Requirement already satisfied: who_is_on_my_wifi in /home/harein/.local/lib/python3.8/site-packages (1.2.0)
Requirement already satisfied: getmac in /home/harein/.local/lib/python3.8/site-packages (from who_is_on_my_wifi) (0.8.2)
Requirement already satisfied: python-nmap in /home/harein/.local/lib/python3.8/site-packages (from who_is_on_my_wifi) (0.6.1)
Any suggestions on how i can avoid this can continue executing my script ?
EDIT
The script finally executed the way i want by changing the,
python scanner.py to python3 scanner.py
You guys were right, it was the way how i executed the script that generated this error and it was not a problem in the module apparently.
I would like to thank everyone who gave the support.<3
When trying to
import this_is_not_a_module
the error you get:
ImportError: No module named this_is_not_a_module
is the error raised by Python 2.
Python 3 would raise a different one:
ModuleNotFoundError: No module named 'this_is_not_a_module'
So, your actual problem is that your system tries to execute your script with some Python 2 version, while you installed your module for your Python 3.8 version.
sometimes you can't import modules because you have installed two python versions(or conda along with it). if you have, delete one of your python versions or activate conda and try importing your module, or just try:
pip uninstall who_is_on_my_wifi
pip install who_is_on_my_wifi
I usually tend to install modules per project and use virtualenv for it.
It kind of links respective versions of programs (like python interpreter, pip and so on) and takes care of PYTHONPATH and the way of installed dependencies.
$ pip3 install virtualenv
$ virtualenv whoisonwifi
$ source whoisonwifi/bin/activate
$ pip --version
pip 21.0.1 from
/mnt/devel/workonhome/whoisonwifi/lib/python3.7/site-packages/pip
(python 3.7)
$ pip install 'who_is_on_my_wifi'
from there your code (sort of) works for me.

ImportError: No module named 'ase.build'

In ubuntu 16.04 i installed python and modules:
sudo apt install python3 python3-scipy python3-numpy python3-ase
then i try to follow the first tutorial on the ASE homepage. I run python3 in bash terminal, and can import other modules but not ase-build. It looks like this:
>>> from ase.optimize import QuasiNewton
>>> from ase.build import fcc111, add_adsorbate
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'ase.build'
>>>
Using a python script throws an equivalent error.
What could be the problem?
UPDATE & SOLUTION
Seems this was not really even a python problem. I seem to have had some package dependency errors probably due to not running apt update in a long time between program installations. I removed python2.x and python 3.x, then iterated apt update, apt upgrade, apt autoremove, then reinstalled only python3. I installed python3-pip and installed the numpy, scipy, and ase packages using the proper form python3 -m pip install --upgrade <package>. Now everything works as expected.
Check what version of the library you have.
import ase
print(ase.__version__)
If the version is 3.10.0 then that is the problem since the build module appeared (as far as I know) in the 3.11.0 version.
Due to this link - you have installed the 3.9.1.4567-3 version on your computer.
But The asu.build has been added in:
commit 71c9563e423e2add645c26f8d0a722f3db13e135
Author: Jens Jørgen Mortensen
Date: Tue Apr 12 15:40:59 2016 +0200
Move stuff to ase.build module
So, the module asu.build doesn't exist in your version (3.9 has been released in 2015). You have to install the newer version of python3-asu.

Cannot run anything from mysql-utilities: "No module named mysql.utilities.common.tools"

RedHat 6.5. Installed via RPM repos the mysql-utilities 1.3.6 and mysql-connector 1.1.6 packages. mysqlrplcheck could be executed (though I never found out if it actually worked). Then I realized that version of the suite is missing what I really need, which is mysqlrplsync. So I downloaded and installed 1.5.4 directly from Oracle. Found out mysql-connector 1.1 was too old and thus upgraded to 2.1.2 of that suite.
Now if I run any of the suites programs, I get:
Traceback (most recent call last):
File "/usr/bin/mysqlrplcheck", line 24, in <module>
from mysql.utilities.common.tools import check_python_version
ImportError: No module named mysql.utilities.common.tools
I think there should be a mysql.py/mysql.pyc in the <pythonlibpath>/ directory, but there is none. So is this a silly packaging error on Oracle's part?
Note: Not a duplicate of 19247867 which wasn't really answered anyway. Different environment, (significantly) different versions of the software.
Not a duplicate of 24267017 nor its referral because the connector is definitely installed. (Though that might be the problem... see my comment)
UPDATE: Possibly fixed in mysql-utilities 2.1.3. See last comment at bug report: https://bugs.mysql.com/bug.php?id=77819
For anyone getting this error on Ubuntu, installing the .deb from oracle or using apt-get did not work for me, what did work was:
wget https://dev.mysql.com/get/Downloads/MySQLGUITools/mysql-utilities-1.5.6.tar.gz
tar -xvf mysql-utilities-1.5.6.tar.gz
cd mysql-utilities-1.5.6
sudo python2.7 setup.py install

Python finds glib, but not _glib

I try to import glib in Python (2.7) and get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/share/pyshared/glib/__init__.py", line 22, in <module>
from glib._glib import *
ImportError: No module named _glib
Seems as if glib is found, while _glib is not found.
Has anybody a hint for solving this problem? I'm running Ubuntu and installed python2.7, python-gobject, python-gda, dbus from Ubuntu's package manager.
I had this issue.
You can try moving the glib and related folders out of the python2.7 install locations e.g. on ubuntu /usr/shared/pyshared in case these have been installed wrongly.
Then remove and reinstalling the related packages with the platform package manager ...
sudo apt-get remove
followed by sudo apt-get install
for ...
python-gobject python-gtk libglib2.0-dev python-cario python-gi
since if they are present in pyshared I found that they were not probably reinstalled
I run ubuntu 10.04, with its native python 2.6.6 and GCC 4.4.5, and an "import glib" in python appears to work.
I installed "python-numpy", "python-matplotlib" and "python-pyfits" with the synaptic package manager and nothing else, in particular nothing installed from a more recent version found on the package's site because previous experience learned me this was usually a very bad idea.
But I have NO /usr/share/pyshared/glib/ folder and do not know how you get it.
Is this folder on your path or PYTHONPATH ?
If yes, try to remove it from the path and retry the "import glib", it is possible that you get some second installation of glib in that place (installed - and maybe needed - by another package).
glib is an interface to the c compiler, so it is used in several places. What is your GCC version ? GTK also makes intensive use of glib. Did you installed "GTK" and "pygtk" from the synaptic or did you did it on the hard way, with all separated little packes to be compiled ? If yes, try to install it from the synaptic.
I mostly think this comes from some installation mix of some python extension somewhere. The radical solution, of course, is to reformat the computer with a fresh O.S., but it's probably something you do not want, so I should try to uninstall all packages python uses, and re-install them from the synaptic.
Hope this helps...

Problems importing python-Xlib

I installed a new module and it appears as if one of its dependencies was not already installed. The module is called Xlib.display.
Here is the error message I received:
from Xlib.display import Display
ImportError: No module named Xlib.display
Where can I find this module that I am apparently lacking? Google yielded no leads.
"Edit: I already have that sourceforge module downloaded but I still get the same results.
Please try.
This shall install Xlib
sudo apt-get install python-xlib
Then you can check
>>from Xlib.display import Display
To install PyMouse if you want to control and capture mouse events please use:
sudo easy_install https://github.com/pepijndevos/PyMouse/zipball/master
Below worked for me!
pip install python3_xlib
I have also used pyuserinput for automation which requires this.
I was having the same problem, but the solutions above didn't work for me. Since I had installed python through the anaconda package, when I used:
sudo apt-get install python-xlib
Xlib was still undetectable by python2. The solution in my case was to use:
anaconda search -t conda python-xlib
Then find the package from the anaconda api, mine was erik/python-xlib. Install it using:
conda install --channel https://conda.anaconda.org/erik python-xlib
Then it worked.
On Debian systems install python-xlib.
On other systems there's a high probability that the package carries the same name.
I don't think the Xlib library works in Python 3.
Source:
Requirements
The Python X Library requires Python 1.5.2 or newer. It has been tested to various extents with Python 1.5.2 and 2.0 through 2.6.
I honestly cant explain why this works... but here is the command that got it working for me.
sudo apt-get install python3-xlib
Should not work because xlib apparently does not work with python 3.x, but everything installed alright, so I'm not complaining!
I was looking for the same answer, however after some more digging it seems that XCB (X protocol C-language Binding) will obsolete Xlib in general. From the XCB website:
The X protocol C-language Binding (XCB) is a replacement for Xlib featuring a small footprint, latency hiding, direct access to the protocol, improved threading support, and extensibility.
Fortunately there are python bindings available as python-xpyb in apt or xpyb on PyPi. I've not gotten that far in my project so I haven't tested if this works with Python3, but this is probably the way to go and the proper place to file any Python3 support bugs if necessary.
Scenario:
I was trying to use screenshot functionalities of pyautogui package. I was getting this error:
Traceback (most recent call last):
File "test_screenshot.py", line 1, in <module>
import pyautogui
File ".../miniconda3/envs/myenv/lib/python3.7/site-packages/pyautogui/__init__.py", line 152, in <module>
from . import _pyautogui_x11 as platformModule
File ".../miniconda3/envs/myenv/lib/python3.7/site-packages/pyautogui/_pyautogui_x11.py", line 7, in <module>
from Xlib.display import Display
ModuleNotFoundError: No module named 'Xlib'
Python code (test_screenshot.py):
import pyautogui
img = pyautogui.screenshot('test.png')
Environment:
Ubuntu 16.04 (LTS)
conda 4.5.11
Python 3.7 (Miniconda)
requirements.txt:
certifi==2019.3.9
Pillow==5.4.1
PyAutoGUI==0.9.42
PyGetWindow==0.0.4
PyMsgBox==1.0.6
PyRect==0.1.4
PyScreeze==0.1.20
PyTweening==1.0.3
Solution:
I installed python-xlib package in the conda environment using:
pip install python-xlib
Now test_screenshot.py is running without any error.
Updated requirements.txt:
certifi==2019.3.9
Pillow==5.4.1
PyAutoGUI==0.9.42
PyGetWindow==0.0.4
PyMsgBox==1.0.6
PyRect==0.1.4
PyScreeze==0.1.20
python-xlib==0.25
PyTweening==1.0.3
six==1.12.0

Categories

Resources