Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import a module in Python

Tags:

python

I have an improperly packaged Python module. It only has __init__.py file in the directory. This file defines the class that I wish to use. What would be the best way to use it as a module in my script?

** EDIT **

There was a period (.) in the the name of the folder. So the methods suggested here were not working. It works fine if I rename the folder to have a valid name.

like image 711
gat Avatar asked Feb 14 '23 13:02

gat


1 Answers

That's not improper. It's a package.

http://docs.python.org/2/tutorial/modules.html#packages

package/
    __init__.py
yourmodule.py

In yourmodule.py, you can do either of the following:

import package
x = package.ClassName()

Or:

from package import ClassName
x = ClassName()
like image 172
FogleBird Avatar answered Feb 16 '23 02:02

FogleBird