Kevin
Kevin

Reputation: 2688

Python3 Imports Issue

So... I am attempting to teach myself Python.

In such, I am attempting to build something that I appear to have no clue about...

I have a "workingdir" structure such as:

/
-- classes/
-- -- install
-- myfile

In myfile I am simply attempting to "import" the file install by using: import classes.install

Which fails with: ImportError: No module named 'classes.install'

I have attempted the following as well, and all end the same way, with the same error:

import .classes.install

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import classes.install

As well as putting an empty __init__.py file inside the classes directory

the file install simply contains:

class gyo_install():

    inst = False

    # check if we have everything we need installed.
    def __init__():
        print("Hello World")

What am I doing wrong? I've searched and searched and searched, everything I see points to the same solutions I've attempted, and none of them work.

Upvotes: 2

Views: 67

Answers (2)

Nestor Yanchuk
Nestor Yanchuk

Reputation: 1246

Create __init__.py inside install directory.

Explanation: You can import from a file that is in your current directory or from a package. A package is a directory with __init__.py inside. In fact, a package can contain only this single file.

You can read the documentation for further information.

Upvotes: 2

Ron Norris
Ron Norris

Reputation: 2690

Python looks for files with a .py extension when importing modules. So a file named myfile will not be recognized simply by the command import myfile. The pythonic way to ensure that the interpreter will find the module is to ensure it has a .py extension. Renaming myfile to myfile.py and install to install.py and then changing the import command to

import classes.install

should solve the problem.

Upvotes: 2

Related Questions