user1513192
user1513192

Reputation: 1153

How to make a python Package?

Here is my structure,

main.py
folder1\
    button.py
    folder2\
        picturebutton.py
folder3\
     listbox.py
     folder4\
         customlistbox.py
         folder5\
             hyperlistbox.py

Now,

I have a module called, "widget.py" and I would like to make it accessible to all the modules here so that each module will be able to say import widget or something of the sort. After googling, it appears that I have to make a package to do this.

I could not function with the examples online as I have no idea how they work, and I am hoping that one of you may be able to help me with my case.

Edit: All the folders, (except for the root one) have an __init__.py file.

Upvotes: 3

Views: 2815

Answers (2)

Julian
Julian

Reputation: 3429

Being able to import some other module does not need for that to be a package, it needs for the widget module to be put on your PYTHONPATH. You'd do that typically by installing it (writing a setup.py file, see the standard library's distutils module).

If you did want a package though, every folder that needs to be a package needs to have an __init__.py file in it (empty is fine).

Upvotes: 2

igauravsehrawat
igauravsehrawat

Reputation: 3954

Proper way is to create a setup.py file for your package but since it may take time . Below is shortcut .

If you want to use your module it frequently like in script . Easy way is to export "PYTHONPATH" in bashrc/zshrc file and give path to the directory containing your code .

For example:

   export PYTHONPATH=$PYTHONPATH:$HOME/path/to/package  

Do check on terminal using

echo "$PYTHONPATH"  

Happy Coding

Upvotes: 0

Related Questions