Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: I can't import a module even though it's in site-packages

I'm following the tutorial "Think Python" and I'm supposed to install the package called swampy. I'm running python 2.7.3 although I also have python 3 installed. I extracted the package and placed it in site-packages:
C:\Python27\Lib\site-packages\swampy-2.1.1
C:\Python31\Lib\site-packages\swampy-2.1.1
But when i try to import a module from it within python:

import swampy.TurtleWorld

I just get no module named swampy.TurtleWorld.
I'd really appreciate it if someone could help me out, here's a link to the lesson if that helps:
http://www.greenteapress.com/thinkpython/html/thinkpython005.html

like image 905
neuroblade Avatar asked Jun 11 '13 17:06

neuroblade


People also ask

Why can't I import modules in Python?

This is caused by the fact that the version of Python you're running your script with is not configured to search for modules where you've installed them. This happens when you use the wrong installation of pip to install packages.

Why am I getting an import error in Python?

The ImportError is raised when an import statement has trouble successfully importing the specified module. Typically, such a problem is due to an invalid or incorrect path, which will raise a ModuleNotFoundError in Python 3.6 and newer versions.

How do you fix an import error?

The import error can be caused by limits set by the program using the file or the amount of available memory on the system. If your import fails because of file size issues, you need to go back and break the file up into smaller files which will enable it to successfully upload. 2.

How do I import a module from another package?

To import the module, you simply have to put the name of the ROS package where the module was installed, followed by the name of the file. That's it!


2 Answers

If anyone else is having trouble with this on Windows, I just added my sites-package directory to my PATH variable and it worked like any normal module import.

C:\Python34\Lib\site-packages

Hope it helps.

like image 61
Joules Avatar answered Sep 23 '22 03:09

Joules


I extracted the package and placed it in site-packages:

No, that's the wrong way of "installing" a package. Python packages come with a setup.py script that should be used to install them. Simply do:

python setup.py install

And the module will be installed correctly in the site-packages of the python interpreter you are using. If you want to install it for a specific python version use python2/python3 instead of python.

like image 29
Bakuriu Avatar answered Sep 23 '22 03:09

Bakuriu