Import a module in Python

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

** EDIT **

The folder name had a period (.). Therefore, the methods proposed here did not work. It works fine if I rename the folder to have a valid name.

+4
source share
3 answers

This is not wrong. This is a package.

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

package/
    __init__.py
yourmodule.py

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

import package
x = package.ClassName()

Or:

from package import ClassName
x = ClassName()
+7
source

__init__.py my_module, , my_module sys.path, from my_module import WHAT_EVER_YOU_WANT

0

It is not necessarily improperly packaged. You should be able to do the from package import Xsame as usual. __init__.pyfiles are modules, just like any other file .py, they just have special semantics as to how they are evaluated in addition to normal use, and basically aliases to the package name.

0
source

Source: https://habr.com/ru/post/1531427/


All Articles