Installed Python Modules - Python can't find them - python

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

Related

ModuleErrorNotFound: no module named 'nltk'

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.

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.

Importing requests module does not work

I have the requests module installed on my system.
pip install requests
Now I am trying to import requests in the rpel
import requests
It fails with the following error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named requests
The most common reason for this is that you have two versions of Python 2.x, and the pip that comes first in your PATH doesn't go with the python that comes first in your PATH.
There are two ways that can happen.
First, you may have, e.g., /usr/local/bin before /usr/bin on your PATH, but your /usr/local copy of Python doesn't have pip. So, when you run pip install requests, that's /usr/bin/pip, which installs into /usr/lib/python2.7/site-packages. But when you run python, that's /usr/local/bin/python, which looks in /usr/local/lib/python2.7/site-packages.
Second, even though your two Python 2.x's are in different locations, they may want to install pip (and other scripts and executables) to the same place. In particular, /usr/bin is usually reserved for stuff that comes with the OS or its package manager, so if you use /usr/bin/python ez_setup.py or /usr/bin/easy_install pip or many other common ways to install pip, it may end up in /usr/local/bin. In which case it will overwrite any earlier /usr/local/bin/pip that went with your /usr/local/bin/python. At any rate, the result is basically the same: pip now means /usr/local/bin/pip, but it still goes with your /usr Python, not your /usr/local Python, and installs into /usr/lib/python2.7/site-packages, which /usr/local/bin/python can't see.
If your two versions are, e.g., 2.7 and 3.4, there's no problem; per PEP 394, either the 3.x versions of everything have to be run with python3 and pip3 and so on, or the 2.x versions have to be run with python2 and pip2 and so on.
If your two versions are, e.g., 2.6 and 2.7, there is a problem, but you can easily work around it, because you should always have pip2.6 and python2.6 vs. pip2.7 and python2.7. You can confuse yourself with python and pip, but you don't have to.
If your two versions are both 2.7, however, there's no way to disambiguate (except by using complete absolute paths all the time, which no one wants to do).
So, why would anyone ever install two copies of Python 2 without knowing what they're doing?
The most common reason is that they're on a Mac, which comes with Python 2.7, but they read a blog post that told them to install another Python and didn't explain how to know what they're doing. Apple's pre-installed Python is in /usr/bin but installs scripts and binaries to /usr/local/bin. The most popular alternative Python versions are the python.org installer and Homebrew, both of which install to /usr/local/bin by default. The fact that Mac users tend to be less Unix-savvy than Linux or FreeBSD users probably doesn't help, but even without that, this is a perfect way to end up with thousands of people who have a pip and a python that doesn't match, and no idea why.
There used to be good reasons for almost all Mac Python users to installing a second Python. Until OS X 10.6, Apple's pre-installed Python versions tended to be badly out of date, and sometimes broken. If Apple's only giving you 2.4, it makes sense to install 2.6. And doing so is no problem, because python2.4 and python2.6 are easy to disambiguate. But Apple has been installing a working 2.7 for years now. There are sometimes good reasons why you need a different one (you need a bug fix in 2.7.7 but Apple gave you 2.7.5, you need a 32-bit build, you need an extra-batteries version like Enthought, you need to build py2app bundles out of it, …), but these reasons do not apply to most people anymore.
In fact, many people on StackOverflow seem to have three versions of Python 2.7. I'm not sure why this is so common, but they'll use Homebrew to install Python 2.7, and then use an installer from Python.org or Enthought, and now they've got three Python 2.7 versions all fighting over ownership of /usr/local/bin.
So, how can you fix this?
If you can use Python 3.x, install that and just use pip3 and python3 (and ipython3 and so on), and paths aren't an issue anymore.
If you don't need a second Python 2.7, get rid of the non-Apple one and just use Apple's.
Otherwise, do not ever use Apple's Python, do not install things for it, do not touch it; just leave it alone for Apple's own tools. If you use Homebrew, its Python should be higher on the PATH (make sure you've got /usr/local/bin before /usr/bin), and it should let you pip install foo without sudo, while Apple's won't, which makes it hard to accidentally screw up and install to the wrong one.
I've also seen at least one Windows user who had both C:\Python27 and D:\Python27, both on the PATH, with the C: one first, but pip only installed for D:. This seems to be far less common than the Mac confusion (probably because Windows doesn't come with Python, and there are no package managers, so the only way you're going to get any Python is by running an installer). And the solution is even simpler here: Windows doesn't need Python, so you can delete whichever one you want.
Finally, on non-Mac *nix systems, especially RHEL/CentOS Linux, you may have a Python 2.6 or 2.4 that's needed by the OS plus a Python 2.7 that you installed because you needed it, or a 2.7 that's needed by the OS and a 2.5 installed as a dependency for some "compatibility" package, or similar. Either way, you can easily accidentally install the pip for the one you don't actually use (especially if you install it with the pip bootstrap instead of your package manager).
The solution here is pretty simple: uninstall that pip, and use yum or apt or whatever to install the python-pip that goes with the Python 2.7 you want to use. And get in the habit of using python2.7 and pip2.7—or just add aliases to your profile so that python or py or whatever you prefer runs python2.7.
For devs with similar problem: Intall python3 version directly from the pack file on their website.
DO NOT DELETE OR EDIT ANYTHING ON usr/local/bin !
rm -rf /Library/Frameworks/Python.framework/Versions/2.7
rm -rf "/Applications/Python 2.7"
Install python3 directly from package on official website.
Reopen VSCode, (if you don't use it, you must) re-install modules in "Not Found" state, run command:
pip3 install requests
and other "not found" modules by command pip3 install xxxxxx
Add "python.pythonPath": "/usr/local/bin/python3" to your settings.json file.
pip install request
-bash: pip: command not found
or
no module name requests
how to fix errors
first:
Download the following and install with python http://get-pip.py
https://bootstrap.pypa.io/get-pip.py
[go to the link and download the python script that I provided and then run it or right click and save as]
then after running script "python space (drag and drop the script in terminal)" run "pip install requests" if you get the error below;
-bash: pip: command not found
See the path mentioned in the warning.
then follow this steps acordilly
see the path in yellow?yeah
/Users/macbookair/Library/... etc
type cd in terminal and then drag and drop the bin folder
1.cd /Users/macbookair/Library/Python/2.7/bin {cd /Users/name/path/Python/version/bin )
./pip install requests
after theses steps
Now run your python script again it should work
up vote me if it helped you.
Are you using Linux and have both python2 and python3 installed?
If so, you installation by:
pip install requests
would install the module to python2
So if you run import in python3, it may cause the problem.
Try to call:
pip3 install requests
to install the module in python3 environment.

how to get the appropriate version of pip

Problem
I can't seem to get my packages to install to the correct site-packages directory.
Background
I wanted to upgrade to python 3.3 but I found that I still need python2.6 in order to use yum (it isn't configured for python3). And so I have both on my system.
Separate pip and easy_install for each version???
The issue is that when I download a package, and run "python setup.py install" I find that it is installed in python2.6. I have tried using pip and easy_install but they do the same. I have read that a solution is to have different versions of pip (pip_2.6 and pip_3.3). But I can't find a way to download the separate versions. One work around is to give the "-d PathToSitePackages" argument in pip but this is inelegant.
other programs not using the newer version
Another issue is that I find that when I use systems such as sphinx, they use the old version of python. I can't seem to figure out how to get them to use the newer version of python.
First, Python is explicitly designed to make it easy to have Python X.Y and Python V.W at the same time if X != V. PEP 394 explains the details, but basically, python3 will always mean a 3.x version, and if python also means a 3.x version, python2 will mean a 2.x version. The same should be true for all well-behaved tightly-integrated scripts like pip or ipython.
This is a good thing, because many POSIX platforms (most linux distros, OS X, etc.) include code that depends on some particular version of Python, so you have to be able to have that version, but often you want a newer version for your own code.
On most platforms, even if X == V, things are still easy as long as Y != W, because a default installation will also give you pythonX.Y, and set things up so well-behaved scripts do the same. This part isn't mandated by the PEP, so some linux distros don't do it, or do it in a different way. But fortunately, you just want 2.6 and 3.3, so you don't have to worry about this part.
So, if you just install pip in any of the normal ways using Python X.Y, , you will get some new packages in Python X.Y's site-packages, probably a script named pip-X.Y (note that it's a hyphen, not an underscore), and usually a symlink pip and/or pip3 to that script. So, you don't have to do anything to get this.
In particular, testing this sequence (the way the documentation recommends for system-wide installation):
$ curl -O https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py
$ sudo python3.3 ez_setup.py
$ curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
$ sudo python3.3 get-pip.py
… I ended up with exactly that.
If you didn't, you'll have to give us details on your platform, how you installed each Python (presumably 2.6 came as part of the system installation, but not 3.3), and how you installed each pip.
If you instead did sudo yum install python-pip and sudo yum install python33-pip (or python3-pip), exactly what you get is up to the distro, so it's possible that you'll end up with just, say, pip (for 2.6) and pip-3.3 (for 3.3), rather than pip-2.6 and pip-3.3 and symlinks pip and pip3. Looking at the RPM contents for various distros' packages, it looks like the standard name for Red-Hat-like systems is python3-pip, with a symlink to pip-python3, and sometimes other names besides.
Anyway, unless you mix and match different methods, you're almost certain to get some way to distinguish the two pip versions.
If worst comes to worst, and you've just got a script named pip, and whichever one you installed last overwrote the one you installed first… You can always install the first one, cp pip pip-2.6, install the second, and mv pip pip-3.3; ln -s pip-2.6 pip; ln -s pip-3.3 pip3.
Or, if you can't even do that, the script is trivial, something like this:
#!/usr/local/bin/python3.3
# EASY-INSTALL-ENTRY-SCRIPT: 'pip==1.2.1','console_scripts','pip-3.3'
__requires__ = 'pip==1.2.1'
import sys
from pkg_resources import load_entry_point
if __name__ == '__main__':
sys.exit(
load_entry_point('pip==1.2.1', 'console_scripts', 'pip-3.3')()
)
And you can just make a copy and s/3.3/2.6/g the copy (you may also have to replace the 1.2.1 if you somehow installed different versions of pip into the different site-packages) and it will work.
But it really, really shouldn't come to this, however. Both setuptools and pip respect PEP 394, and any package manager that doesn't has to have some rules or its python 3 packages are useless. So, if you think these tricks are necessary, you probably did something wrong earlier.

How to install pip in a new python installation

I recently installed python 2.7.2 on my Mac running OSX 10.6.8. Previously, I had version 2.6. I set my path in .bash_profile as follows:
export PATH=/usr/local/bin:$PATH
export PATH=/usr/local/share/python:$PATH
so that when I run python it will refer to my new installation. It does.
I would also like to use pip with my new installation, but the problem is that I already have the current version of pip installed at
/usr/local/bin/pip.
I tried to re-install pip with:
easy_install pip
But, of course this does not put pip in the desired new directory
/usr/local/share/python/pip
but simply refers to the existing version in /usr/local/bin/pip.
Can someone tell me how to fix this?
I would like to then use pip to install NumPy and SciPy in the correct directory (I was having trouble getting the SciPy installation to work with my old version of python, hence the new install).
If you'd like, you can visit the website where I found instructions for installing python 2.7, creating/updating my .bash_profile, installing pip, and NumPy and SciPy. Might provide some insight, or I'm happy to give more details if needed. Thanks!
http://www.thisisthegreenroom.com/2011/installing-python-numpy-scipy-matplotlib-and-ipython-on-lion/#python
Install distribute as per the instructions at http://pypi.python.org/pypi/distribute .
Make sure you specify the full path to the python executable (/usr/local/share/python/python or smth in your case).
$ curl -O https://svn.apache.org/repos/asf/oodt/tools/oodtsite.publisher/trunk/distribute_setup.py
$ /usr/local/share/python/python distribute_setup.py
Then you should have /usr/local/share/python/easy_install.
After that, run:
$ /usr/local/share/python/easy_install pip
Then you should have /usr/local/share/python/pip.
Depending on the ordering of things in your PATH, either your old, or the newly installed pip is executed when you execute the pip command, so you either might have to adapt your PATH, or specify the full path to /usr/local/share/python/pip when installing eggs.
(shameless plug:
In any case, you might consider using virtualenv for installing packages into a "project" specific isolated environment, as opposed to installing them globally.)
I needed to uninstall brew's python.
Then, I was left with python v2.7.6
Next to install, pip I ran
sudo easy_install pip
installed fine and working
I had a similar issue, try this:
$ python -m pip install --upgrade --force-reinstall pip
This will force reinstall pip with whatever version of python you use including installing the binary.
A few days ago I had a friend who was starting Python Programming and needed help with the same issue: installing pip. There are debates over which one to choose between easy_install and pip and it seems everybody is heading the pip direction. Either way, installing either of them can be frustrating.
You can use this simple tutorial : installing pip package manager the easy way
Here are what you should keep in mind as you follow the above guide:
If you already have an older version installed, uninstall it or totally remove the python installation
Once that is cleared, download an install Python.
After that, download ez_setup.py file and save it to your desktop - easily accessible from the command line
Now run it from the command line and it will install easy_install for you after which,
You can use it to install pip.
Once again, you can do this or use the above link to find a simple step-by-step guide on how to get it installed on your computer.
Good luck.
Just so that people knew, ATM we can install PIP by downloading get-pip.py from the page with docs and run it like this:
c:\python27\python.exe get-pip.py
BTW, Python 3.4 comes with PIP pre-installed.
One of the command line options lets you choose where to install to.
--install-dir (-d) install package to DIR
So something like - # easy_install pip -d /usr/local/share/python
(Please correct me if I'm wrong.)
Just wanted to say that I found a way to get around my problem. I don't know that I can explain it perfectly, since I am not very good at understanding what I am doing with this stuff just yet! But, the problem seems to have been with my PATH. I removed the PATH that I posted in my original question, and then used easy_install pip. It went straight to python 2.7.2 (my new version) with no problem. I then successfully used pip to install NumPy and SciPy in the correct location, and they both work. Thanks to ErikAllik and FakeRainBrigand for taking the time to look into it!

Categories