ModuleErrorNotFound: no module named 'nltk' - python

I'm trying to write a basic script using nltk, which I've already installed with pip on my computer, but whenever I try to run my code with import nltk at the top I keep getting a module not found error, this occurs with other programs I write as well, always at the first line, and I'm not sure what to do.

It's a common issue. You probably have multiple python versions and virtualenvs, so the pip install might be installing packages for the wrong version. It's always best to use pip this way:
<python_command> -m pip install <package>
where python_command is the same one that you run your scripts with, i.e. python or python3. This way you can be 100% sure that the package will be installed for the right python version / virtualenv.
See this answer for more detailed explanation.

Related

No module named "pyautogui" after using pip install and it appearing on pip freeze

I have looked at other similar questions but I got no answer.
I have installed pyautogui through pip install and I have checked using pip freeze and when I try to import this module I get the error "No module named 'pyautogui'".
I have also tried to restart the IDE that I am using (Pycharm) and I still don't know what is going on.
Does anyone have any ideas?
Before doing anything else, try restarting your IDE. Sometimes it's as simple as the module wasn't available when it started up.
Otherwise, it is possible that your pip installation is running under a different version of python than the IDE you are using. Some computers come with python pre-installed, and some IDEs might install a new version as a dependency, so you can end up with two (or more) different copies of python.
The easiest solution is probably going to be to install the package from within Pycharm, which will use the same copy of python: https://www.jetbrains.com/pycharm/guide/tips/install-and-import/#:~:text=PyCharm%20can%20do%20both.,according%20to%20your%20project%20styles.
Alternatively, you can try opening the python interpreter and importing the module there:
$ python
>>> import pyautogui
If that also fails, then installing pyautogui in the shell by running:
$ python -m pip install pyautogui
might solve it, if your IDE uses the same installation that is on your $PATH, but not the same as pip.
If that doesn't work, then you're going to need to figure out which python installations are being used where.

I'm confused by pip installations

I have recently begun having troubles using pip to install python packages. I have always used pip but never really understood how it actually works, my experience with it is basically limited to "pip install pkg".
Recently when trying to install openCV on my machine, I followed a few guides that involved changing paths etc. Since making these changes I have been having trouble using pip to install packages correctly.
Now when I run "pip3 install pkg", the install runs fine without any errors. When I try to import the module in python however, python cannot find the package. If I run "pip3 list" in the terminal I get a list of modules that is different to running help('modules') within python.
I think pip is installing the packages to a different location than my version of python is referencing when importing modules?
Is there a way I can change where pip installs to? What did it mean to change paths and how can I avoid this in the future?
Thanks in advance.
EDIT: I should mention that running "python3 -m pip install pkg" installs the packages correctly.
Because you have 2 versions of python installed, the best solution is to install and use virtualenv
A Virtual Environment is a tool to keep all dependencies required by different projects and python versions in separate places. It solves the problem you mentioned and keeps your site-packages directory manageable.

How to install pymssql to Python 3.4 rather than 2.7 on Ubuntu Linux?

I'm not overly familiar with Linux and am trying to run a Python script that is dependent upon Python 3.4 as well as pymssql. Both Python 2.7 and 3.4 are installed (usr/local/lib/[PYTHON_VERSION_HERE]). pymssql is also installed, except it's installed in the Python 2.7 directory, not the 3.4 directory. When I run my Python script (python3 myscript.py), I get the following error:
File "myscript.py", line 2, in
import pymssql
ImportError: No module named 'pymssql'
My belief is that I need to install pymssql to the Python 3.4 folder, but that's my uneducated opinion. So my question is this:
How can I get my script to run using Python 3.4 as well as use the pymssql package (sorry, probably wrong term there)?
I've tried many different approaches, broken my Ubuntu install (and subsequently reimaged), and at this point don't know what to do. I am a relative novice, so some of the replies I've seen on the web say to use ENV and separate the versions are really far beyond the scope of my understanding. If I have to go that route, then I will, but if there is another (i.e. easier) way to go here, I'd really appreciate it, as this was supposed to just be a tiny thing I need to take care of but it's tied up 12 hours of my life thus far! Thank you in advance.
It is better if when you run python3.4 you can have modules for that version.
Another way to get the desire modules running is install pip for python 3.4
sudo apt-get install python3-pip
Then install the module you want
python3.4 -m pip install pymssql
The easiest way is to use virtual environments instead of system paths or environment scripts. See official Python package installation guide.
All you need to do is to
# Create fresh Python environemnt
virtualenv -p python3.4 my-venv
# Activate it in current shell
source my-venv/bin/activate
# Install packages
pip install mysqlclent
Note that mysqlclient is Python 3.x compatible version.

unable to use pip for installing python packages

I have just installed python 3.4, including pip, on a win64 machine.
When trying to use pip to install a package, I encounter a strange error.
I'm running:
pip install packagename
and get:
Can't locate pip.pm in #INC...
This looks like a perl error to me. I do have perl installed on my machine, but why is windows trying to invoke perl?
Waiting for your advice,
Thanks!
I think you have installed pip perl module, and it added the pip perl tool in your PATH variable (in upper place than pip from python). So, if you don't use that perl module, you can delete it, or call the python pip tool with the complete path
OK, so what did it for me was changing the name of the pip file found somewhere on my drive to pip_pm. This convinced windows to just use the python pip command. Thanks for your help anyway.

Installed Python Modules - Python can't find them

This is a beginner python installation question. This the first time I have tried to install and call a package. I've got pip installed, and I tried to install two modules - numpy and pandas.
In terminal, I ran the following commands:
sudo pip install numpy
sudo pip install pandas
Both commands returned with a success message. Here is the pandas success message (it's the second package I installed and was still in my terminal history):
Successfully installed pandas
Cleaning up...
pip returned a similar message after numpy was installed.
Now, when I launch python and try to call it with:
import pandas
I get this error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pandas
Same when I try numpy.
Can anyone tell me what I'm doing incorrectly?
argh. you've got two pythons in your path that are the same version? don't do that.
pip, easy-install, etc are associated with a particular python install and will use that python by default. so if you have a system-provided python and a system-provided easy_install (or installed easy_install yourself using the system python) then easy_install will, by default, install packages for the system python.
the best way to avoid this mess, imho, is to use use system python for that version (2.7 probably) and, for other versions, use make alt-install when installing, which will give you executables like python3.1 and the like. if you really need to replace the version provided by the system, uninstall it.
once you've done that. each python will have a distinct name (ending in the version) and python will remain the system one.
next, when you install easy_install, you'll notice that there are version-specific versions (easy_install-2.7 for example). use those. if one is missing, then install distutils with the appropriate python (eg use python3.1 and you'll get an easy_install-3.1). unfortunately, each time you do this (iirc) you overwrite the un-versioned easy_install, so never use that - always use the versioned one.
alternatively, you could not install easy_install or pip for anything other than the system version, then always use virtualenv. virtualenv will let you specify a python version (so you can use the system virtualenv for all pythons installed) and then installs easy_install/pip for the python you use. so once you're inside the virtual environment, everything just works.
and i just realised i haven't much experience with pip, so i can't actually help with that (except to note that virtualenv does provide it) (about which is preferable: it used to be that pip was better maintained; i think these days the latest distutils/easy_install is as good as pip, but pip has a few more features that i have never used).
disclaimer: the above is from experience gained developing lepl, which runs on 2.6 to 3.2, so i need to test it on all those. as far as i know, what i describe above works for me, but i have no deep knowledge of python/easy_install/pip so i may have some mistakes in rationalising/describing things (in other words, i'm writing all this in case it helps, but i'm a bit worried i have an error - please, someone correct me if so).
With this, I solve the problem (may help you):
$ sudo apt-get install python-pandas
$ sudo apt-get install python-numpy

Categories