Python can't find module NLTK - python

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

Related

ImportError: No module named xgboost

When I tried import from python terminal I get this error, although I followed all the steps to install xgboost, somehow python is unable to get the package details.I am relatively new to python, I could easily install numpy and pandas packages,I used this link for installation on MACOSX http://xgboost.readthedocs.io/en/latest/build.html
>>> import xgboost
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named xgboost
When I did pip install xgboost, I am getting this error,
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/h7/pyph_7qj6171tqp50cf2xc7m0000gn/T/pip-build-TEvbD6/xgboost/
I am getting this on printing sys path,
print sys.path
['', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Python/2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']
First you need to get control of your python environment. Download the homebrew python by pasting these into a fresh terminal window
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
here you will be prompterd to enter your password. After homebrew is installed, install python with brew install python. Please check your installation with brew doctor and follow homebrew's suggestions.
Now, with a fresh terminal window, install xgboost from pip. Open terminal and paste this in:
pip install xgboost
I had the same issue. I tried everything but the only solution that worked for me to was to install the whl file directly from here :
http://www.lfd.uci.edu/~gohlke/pythonlibs/#xgboost
then you can do :
pip install yourFile.whl
On windows I managed to just double click on the whl file and install it
Good luck
FYI if you are using anaconda dist'n then need to do
conda install -c conda-forge xgboost
Use conda install in Anaconda Powershell Prompt then use pip install
conda install -c conda-forge xgboost
pip install xgboost
git clone --recursive https://github.com/dmlc/xgboost
cd xgboost
sudo cp make/minimum.mk ./config.mk;
sudo make -j4;
sh build.sh
cd python-package
python setup.py install
Atleast Now I can import xgboost from terminal on macosx, I haven't been able to import it in jupyter notebook as yet.
Try running
pip install xgboost
in Anaconda prompt; it's important that you do it in Anaconda prompt so it is in same location as the Python you're using.
I tried
pip install xgboost
and
pip3 install xgboost
But it doesn't work
ModuleNotFoundError: No module named 'xgboost'
Finally I solved
Try this in the Jupyter Notebook cell
import sys
!{sys.executable} -m pip install xgboost
Results:
Collecting xgboost
Using cached xgboost-1.4.1-py3-none-win_amd64.whl (97.8 MB)
Requirement already satisfied: scipy in c:\programdata\anaconda3\lib\site-packages (from xgboost) (1.5.2)
Requirement already satisfied: numpy in c:\programdata\anaconda3\lib\site-packages (from xgboost) (1.19.2)
Installing collected packages: xgboost
Successfully installed xgboost-1.4.1
Write this on the terminal of Jupyter:
conda install -c anaconda py-xgboost
On Pycharm you can go to Pycharm > Prefernces, go to the interpreter you have and install the xgboost package.
Give the following commands a try.
pip install xgboost
pip install plotly
All you need within Jupyter Notebook cell is:
try running
import sys
!{sys.executable} -m pip install xgboost
Open the terminal
Type pip install xgboost and hit enter.
Make sure your internet connection is good.
Do not forget to crosscheck the successful installation of it in jupyter.
for ubuntu 18 LTS, this worked for me.
cmake is a pre-requiste to it, so if you havent you would need to install that, currently at print its version 3.10 + minimum.
in terminal:
sudo snap install cmake --classic
in jupyter notebook(if terminal just edit it)
import sys
!{sys.executable} -m pip install xgboost
if you run
pip install xgboost
in command line and no result, run the same command in jupyter notebook.
In my case, I had multiple Python versions, and I was installing it in the wrong one.
pip install xgboost
installed the module on Python 3.7, but I needed to install it with Python 3.10. What I run was:
py -m pip install xgboost
which worked, since "py" is my binary for Python 3.10. The suitable command if you have multiple Python versions may be different depending on which version you have. In Ubuntu, for example, this worked for me:
python3.8 -m pip3 install xgboost
Windows 10, Run Anaconda as Administrator.
Launch Powershell Prompt from Navigator
pip install xgboost

ImportError: cannot import name wraps on Mac

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.

Installing & running modules in Python 3 (Beginner)

I am very new to the world of coding, so I will try to provide as much information as i can regarding to my question.
Essentially, I wanted to install a module (moviepy) for Python 3. The site were I found the module suggested I use pip to unpack and install the module, so I did.
In my terminal, I entered pip install moviepy and pip proceeded to unpack and install my module, yay!
I then went over to my IDLE to see if the module would import, import moviepy, but received this error:
ImportError: No module named 'moviepy'
Huh? I thought I had just installed moviepy?
Upon further investigation, the module appears to have been written to my Python 2.7 site-packages folder and not my in Python 3 site-packages folder.
So my question is: How can I get my module to install to Python 3?
The modules website says that it is compatible with Python 3.
Im assuming this is a file path issue of some kind, but i don't know where to begin.
I'm currently using a OS X Yosemite version 10.10.2, Python 2.7.6, Python 3.5.0
Any help or comments are greatly appreciated here!
Help the n00b!
If you are using python in linux you must run pip with python3:
python3 -m pip install moviepy
according to python official doc :
On Linux, Mac OS X and other POSIX systems, use the versioned Python
commands in combination with the -m switch to run the appropriate copy
of pip:
python2 -m pip install SomePackage # default Python 2
python2.7 -m pip install SomePackage # specifically Python 2.7
python3 -m pip install SomePackage # default Python 3
python3.4 -m pip install SomePackage # specifically Python 3.4
Since pip itself is written in python , you could simply run the following in your terminal:
/path/to/python3 /usr/bin/pip install foo
More info:
To install pip, securely download get-pip.py
Run the following (which may require administrator access):
/path/to/python3 get-pip.py
Try below version of moviepy
pip install moviepy==0.2.3.5

Installing requests-kerberos on Windows

Is there any way to install requests-kerberos on Windows?
When I try to install it with pip I have the following error:
py -m pip install requests-kerberos
ImportError: No module named 'commands'
Command "python setup.py egg_info" failed with error code 1 in C:\Users\user1\AppData\Local\Temp\pip-build-n8s_inn\kerberos
I would be grateful if anyone knew another kerberos module. I'm using Python 3.4 and Windows 8.
Best regards.
The commands module was deprecated in python 2.6.
Thus, the issue appears to be a problem with your pip installation, and not with requests-kerberos.
There are a few things you can try, but I would focus on ensuring pip is working correctly. While you could install the package manually, you are really just pushing the pip problem down the road until the next time you install a package.
Ensure pip is installed correctly.
Use the pip command to ensure you are running pip in the python 3.4 context: (Note, this is my output, yours will be different because you are on Windows and running 3.4)
$ pip --version
pip 1.5.6 from /Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg (python 2.7)
Additional information about pip can be found here.
Download and install manually
Download the package manually from the pypi repo.
Download the .tar.gz
Extract the tar.gz and run python setup.py install

If I install python scikit-learn through pip, I get latest version, but python cannot import module

System: Ubuntu 12.04 (precise)
If I install scikit-learn through
sudo apt-get install python-sklearn
I can import sklearn in python but get version 0.10, which I do not want
If I install scikit-learn through
sudo pip install -U scikit-learn
I can see the scikit_learn-0.14.1-py2.7.egg-info under /usr/local/lib/python2.7/dist-packages/
but I don't know how to get python to import the correct version. Setting PYTHONPATH to /usr/local/lib/python2.7/dist-packages/ did not help.
How do I get python to load the modules from where pip puts them, not where apt-get install puts them?
You can set the site dir in your code.
import site
site.addsitedir('/usr/local/lib/python2.7/dist-packages')
It might be the case that you have several versions of python installed and the pip command in your PATH is not matching the python command in your path. You can check with:
which python
which pip
cat `which pip`

Categories

Resources