(Linux)
I just installed eyed3 for python using apt-get, everything installed fine but when I tried to import eyed3 I got a trackback error, no such module. Do I need to do something special to let python know where the module is at or do I need to move the module manually. Oh, I tried this with python 2.7 and 3 and I got the same result.

On the same topic, if I write my own module how can I set it up to be imported as if it was a built in module rather than having to have a copy in every project directory?

Wasnt sure if this belonged here or in the Linux section.

Recommended Answers

All 7 Replies

Try

import eyeD3

To answer your second question, create for example a directory with

$ mkdir ~/pymods

in a terminal, then add the following lines to the file ~/.bashrc, and restart the terminal

PYTHONPATH="$HOME/pymods:$PYTHONPATH"
export PYTHONPATH

Now every python file added to the directory ~/pymods becomes importable
from anywhere (under your user name). For example create a file
~/pymods/abracadabra.py and use import abracadabra from anywhere.
You can also store python packages in pymods (directories containing a file __init__.py)
You can also add symbolic links to python files and packages.

There are other techniques for this, but it should be a starting point.

Thanks. That should workout good. Could you explain the __init__.py line a little more though please.

Another way is to create a folder like MyModules in a path
that Python has on its PYTHONPATH eg. C:\Python27\MyModules
Now save your module eg. module1.py in there.

You also have to create an __init__.py file (can be empty) in that folder,
so that Python assumes that it's dealing with a package name.

In your programs use for instance ...
import MyModules.module1 as module1

Remember that module names are case sensitive.

Sorry, I lost my Linux machine several years ago.

vegaseat If it's a directory that is already in PYTHONPATH, couldn't it already have modules in it? If so, would there already be an __init__.py file?

Gribouillis Will the line PYTHONPATH="$HOME/pymods:$PYTHONPATH" not restore back to it's original state after a computer shutdown or restart? Will I need to add that line to .bashrc or .profile?

A directory in the PYTHONPASS that already has a __init__.py file is a package directory. Many of these directories appear in the site_packages directory. Putting your own modules in there would be possible, but could lead to name collisions. Also, if you pass your program on to other folks, they would not know which module was written by you. Could get very messy!

This might help ...

# show the system path for Python --> PYTHONPATH

import sys

for path in sys.path:
    print(path)

An alternative to what I wrote above is to create a per user site-packages directory (pep 370). In my kubuntu system it means typing

$ mkdir --parents ~/.local/lib/python2.7/site-packages

in a terminal. This directory is then automatically added to the python path when I run python. I can add my own scripts to the directory. For example if I have a file ~/Documents/daniweb/bargr.py, I can add a symbolic link

$ ln -s $HOME/Documents/daniweb/bargr.py $HOME/.local/lib/python2.7/site-packages/bargr.py

then I can use import bargr in python code from anywhere under my user name.

Another thing I can do with this directory is install modules from pypi in it, for example

$ pip install --user tabula

installs the pypi package tabula in my per-user site packages directory.

(I don't know what it does and there is no documentation, so

$ pip uninstall tabula

works too :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.