ImportError: cannot import name wraps on Mac - python

I am trying to run Python unit test using mock on IDE: PyCharm.
Since I am using Mac (El Capiton), I am unable to do:
pip install mock
Therefore I did:
sudo -H pip install --ignore-installed six
sudo -H pip install --ignore-installed mock
It installed successfully. Now when I write a test, it throws following error:
File "/Library/Python/2.7/site-packages/mock/__init__.py", line 2, in <module>
import mock.mock as _mock
File "/Library/Python/2.7/site-packages/mock/mock.py", line 68, in <module>
from six import wraps
ImportError: cannot import name wraps
How can I resolve this?

Mock requires six version 1.7 or newer. pip show six to get the version and pip install --upgrade six to upgrade.
If that doesn't work, check which version of six is being loaded.
Check the Python Version
thomas:~$ python -c 'import six; print(six.__version__)'
1.10.0
Check the pip Version using pip show six
thomas:~$ pip show six | grep ^Version
Version: 1.10.0
Check the Python Location
thomas:~$ python -c 'import six; print(six.__file__)'
/Library/Python/2.7/site-packages/six.pyc
Delete the python version and pip version are different, delete six.py and six.pyc from the Python Location
Sources:
Stackoverflow
Github
Github

The issue is that there is an old version of six (1.4.1) in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python and in sys.path this directory comes before /Library/Python/2.7/site-packages where the newer version of six is installed. This looks to me like a configuration issue of the python Apple provides.
The easiest solution I could come up with is setting the PYTHONPATH environment variable explicitly to include /Library/Python/2.7/site-packages, so it ends up in sys.path before the /System/... path:
export PYTHONPATH="/Library/Python/2.7/site-packages:$PYTHONPATH"
I suppose installing a correctly configured python in /usr/local would do the trick as well.

Related

ImportError: No module named PySimpleGUI

I am attempting to use PySimpleGUI to create a very simple GUI, but when I run the command "python hello_world.py" I get the error:
File "hello_world.py", line 3, in <module>
import PySimpleGUI as sg
ImportError: No module named PySimpleGUI
I've run the command "pip install PySimpleGUI" and it says "Successfully installed PySimpleGUI-4.55.1." I'm not sure what I'm missing here.
Code snippet here:
# hello_world.py
import PySimpleGUI as sg
sg.Window(title="Hello World", layout=[[]], margins=(100,50)).read()
check where python is installed
import os
import sys
print(os.path.dirname(sys.executable))
>>C:\Program Files\Python310 #here 310 is python version your may be differrent
if this outputs inside C:\Program Files\Python310
check for C:\Program Files\Python310\Lib\site-packages\PySimpleGUI
if its not there then Run CMD as admin and try python -m pip install PySimpleGUI
Update pip to the current version via "python -m pip install --update pip".
It's strange, but my PySimpleGUI module became visible after I updated the pip from version 21.2.3 to version 22.0.4 via "python-m pip install --update pip".
Before the pip version update, the contents of my local environment looked like this:
(env) D:\work\PycharmProjects\demo>pip list
Package Version
----------- -------
flake8 4.0.1
mccabe 0.6.1
pip 21.2.3
pycodestyle 2.8.0
pyflakes 2.4.0
setuptools 57.4.0
WARNING: You are using pip version 21.2.3; however, version 22.0.4 is available.
You should consider upgrading via the 'D:\work\PycharmProjects\demo\env\Scripts\python.exe -m pip install --upgrade pip' command.
After the update:
(env) D:\work\PycharmProjects\demo>pip list
Package Version
----------- -------
flake8 4.0.1
mccabe 0.6.1
pip 22.0.4
pycodestyle 2.8.0
pyflakes 2.4.0
PySimpleGUI 4.57.0
setuptools 57.4.0
And the program hello_world.py it began to run successfully.
EDIT Dec2021:
This seems to me like an bug with pip rather than PySimpleGUI. Try this https://pip.pypa.io/en/stable/installation/ reinstall pip in the python interpreter using this.
Original Answer:
You can check all the installed packages with
python3 -m pip list
And see if you find PySimpleGUI in it. If yes then python3 hello_world.py should work if not (i suspect latter) run
python3 -m pip install PySimpleGUI
Maybe do this import:
import PySimpleGUI
and then the version of PySimpleGUI instead of:
import PySimpleGUI
Doing this:
C:/Users/Test> pip list
in the terminal, for me, it says there's PySimpleGUI installed.

How to default my python to another python version

I am having a big headache dealing with python versions. I couldn't do pip install xlwt because it was complaining to me about SSL certification problem. So then I installed python 2.7.16 and installed xlwt and got Requirement already satisfied: xlwt in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (1.3.0)
However then when I run python code.py I keep getting ImportError: No module named xlwt although I just did pip install xlwt
what should I do?
The safest way to make sure your pip refers to the correct python version is to call it explicitly from the python version you intend to use, e.g.
/usr/bin/python2.7 -m pip install some-package
If it complains that the pip module is missing, you can install it via:
wget https://bootstrap.pypa.io/get-pip.py
/usr/bin/python2.7 get-pip.py
You can replace /usr/bin/python2.7 with whatever version you want to use.

pip3 install ruamel.yaml[jinja2] breaks ruamel.yaml

I have a docker container that has been using ruamel.yaml and ruamel.yaml.jinja2 for a while. Lately it has begun to fail. The following dockerfile reproduces the problem:
FROM ubuntu
RUN apt update; apt install -y python3 python3-pip
RUN pip3 install -U pip setuptools wheel
RUN pip install ruamel.yaml
RUN python3 -c "from ruamel.yaml import YAML"
RUN pip install ruamel.yaml[jinja2]
RUN python3 -c "from ruamel.yaml import YAML"
The first python3 command succeeds, but the last fails with the following message: ImportError: cannot import name 'YAML'
I have tried specifying earlier versions of ruamel.yaml, but with no success, unless I go all the way back to 0.15.0 (from 2017).
I have also tried different versions of python3.x and pip with no change.
As #AKX indicated ruamel.yaml.jinja2 (which is installed when you do pip install ruamel.yaml[jinja2]) was updated to handle jinja2 comments, and this nested package contained a spurious .pth file breaking the correct ruamel.yaml install.
There is a new, 0.2.4, version of ruamel.yaml.jinja2 on PyPI and with that I have been able to build your Dockerfile without error.
But in general it is a good idea to fix the version numbers of all the packages you use in your Dockerfile, as #AKX suggested.
It looks like ruamel.yaml.jinja2 was updated to version 0.2.3 four days ago.
I assume that version is broken, so instead install the older version of ruamel.yaml.jinja2 manually for the time being:
pip install ruamel.yaml "ruamel.yaml.jinja2<0.2.3"
This seems to work, too.
~ $ docker run -it python:3.7-stretch sh -c 'pip install ruamel.yaml[jinja2]; python3 -c "from ruamel.yaml import YAML"'
Collecting ruamel.yaml[jinja2]
Collecting ruamel.yaml.jinja2>=0.2; extra == "jinja2" (from ruamel.yaml[jinja2])
Successfully installed ruamel.yaml-0.15.97 ruamel.yaml.jinja2-0.2.3
Traceback (most recent call last):
File "<string>", line 1, in <module>
ImportError: cannot import name 'YAML' from 'ruamel.yaml' (/usr/local/lib/python3.7/site-packages/ruamel/yaml/__init__.py)
~ $ docker run -it python:3.7-stretch sh -c 'pip install ruamel.yaml "ruamel.yaml.jinja2<0.2.3"; python3 -c "from ruamel.yaml import YAML"'
Collecting ruamel.yaml
Collecting ruamel.yaml.jinja2<0.2.3
Successfully installed ruamel.yaml-0.15.97 ruamel.yaml.jinja2-0.2.2
~ $
The longer-term fix is to use a proper dependency version pinning system – I like pip-tools.

No module named 'termcolor'

No module named 'termcolor'
pip install termcolor returns
C:\Users\admin>pip install termcolor
Requirement already satisfied: termcolor in c:\python27\lib\site-packages
You are using pip version 9.0.1, however version 10.0.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
Not sure why I can't import this module? Here's the code.
from termcolor import colored
My guess is that you probably have two versions of Python or downloaded the wrong module.
1)Wrong Module:
pip install term color
It will install a different module. To fix it, install the correct module:
pip install termcolor
2)Two-Versions
If you have two versions of Python, you need to specify it:
i)Two versions of Python 3:
If you two versions of Python3, specify:
pip3.x install termcolor
ii)If you have Python 2 installed:
If you have Python 2 and 3 installed, specify it, as pip works for Python2, and pip3 works for Python 3:
pip3 install termcolor
If you have only Python3, or in simple words only one version of Python, You can follow the first instruction.
The simplest way, if you already have termcolor installed to Python3.x and it still has problems when you use programs with #!/usr/bin/env python, is to just copy the termcolor.py file from /usr/lib/python3/dist-packages to /usr/lib/python2.x
Seriously, that's it. No dependencies. Just copy and go.
Are you in the virtual environment? If you do not use virtual env, I recommend you should use it to avoid a lot of problems related to the system.
Install virtualenv for window:
pip install virtualenvwrapper-win
mkvirtualenv venv
Activating virtualenv:
workon venv
Take termcolor.py out of c:\python27\lib\site-packages and place in c:\python27\lib\
might seem strange but it has a great chance to work.
In Linux, I use this command, if using windows maybe see an equivalent.
mv \python27\lib\site-packages\termcolor.py \python27\lib\
I had similar issue when I tried to import termcolor in jyputer notebook.
Adding the path where termcolor is installed has solved my problem:
import sys sys.path.append("THE_PATH_To_YOUR_VERTUUAL_ENVIRENMENT/lib/python3.7/site-packages")

Python can't find module NLTK

I followed these instructions http://www.nltk.org/install.html to install nltk module on my mac (10.6)
I have installed python 2.7, but when I open IDLE and type import nltk it gives me this error
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import nltk
ImportError: No module named nltk
The problem is the module is installed in another python version, 2.6. How can I install the package in python version 2.7? I tried some of the solutions suggested in various answers, for example I tried typing this in the terminal
export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages
and then installed NLTK again with the command
sudo pip install -U nltk
but I get the message: Requirement already up-to-date in /Library/Python/2.6/. So apparently the command line export PYTHONPATH didn't do anything (it still tries to install the package in 2.6) OR (more likely) I didn't understand the meaning/functioning of that command line. What am I doing wrong?
On OS X you could have multiple installation of Python, so investigate it first:
$ which python python2 python3
/usr/bin/python
/usr/local/bin/python3
$ which pip pip2 pip3
/usr/local/bin/pip
/usr/local/bin/pip2
/usr/local/bin/pip3
All within /usr/bin are built-in and all other in /usr/local/bin are external installed by Homebrew or some other package manager.
If you're using pip or pip3 from /usr/local, then you've to use the same Python instance, otherwise they're different instances.
Just install it via pip:
pip install nltk
or for Python 3:
pip3 install nltk
then run the right Python instance from /usr/local/bin or update your PATH system variable.
Make sure you install the actual Python for Mac, not the one built into the console. Then, install pip by executing this script. Then skip to part 3 of the instructions and go from there.
Try this
pip install --user -U nltk
On my mac I had two different versions of Python 3 installed: Python 3.6 and Python 3.7. I had installed nltk having Python 3.7 in my $PATH:
$ pip3 install nltk
$ which python3
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
$ echo $PATH
/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
But nltk was missing for Python 3.6. Solution: install nltk also for version 3.6.
$ echo $PATH
/Library/Frameworks/Python.framework/Versions/3.6/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
$ pip3 install nltk
Collecting nltk
Using cached https://files.pythonhosted.org/packages/6f/ed/9c755d357d33bc1931e157f537721efb5b88d2c583fe593cc09603076cc3/nltk-3.4.zip
Requirement already satisfied: six in /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages (from nltk) (1.12.0)
Collecting singledispatch (from nltk)
Using cached https://files.pythonhosted.org/packages/c5/10/369f50bcd4621b263927b0a1519987a04383d4a98fb10438042ad410cf88/singledispatch-3.4.0.3-py2.py3-none-any.whl
Installing collected packages: singledispatch, nltk
Running setup.py install for nltk ... done
Successfully installed nltk-3.4 singledispatch-3.4.0.3
I would use a virtualenv, but if you really want to use it from the terminal, I'd recommend adding your export statement to ~/.bashrc
Just restart Jupyter Notebook or anything you are using after installing...
It works...
In my case, the following command worked for me. Try this!!
py -3 -m pip install nltk
I had a similar issue an an Intel MacBook.
Fixed with
Make sure the python bin directory in the path
mine is /Users/<user>/Library/Python/3.9/bin
Restart the computer
NOTE that just restarting the terminal did NOT help

Categories

Resources