Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (1.6k points)

I have a directory that stores all the .py files.

bin/

   main.py

   user.py # where class User resides

   dir.py # where class Dir resides

I want to use classes from user.py and dir.py in main.py.

How can I import these Python classes into main.py?

Furthermore, how can I import class User if user.py is in a sub directory?

bin/

    dir.py

    main.py

    usr/

        user.py

1 Answer

0 votes
by (25.1k points)

Firstly create an empty file named __init__.py and save in the directory with your modules. This will indicate python that it is ok to import form this directory. Then you can import files like this:

from user import User

from dir import Dir

Related questions

0 votes
1 answer
0 votes
2 answers
0 votes
1 answer
0 votes
1 answer

Browse Categories

...