0

I'm in the process of learning how to package a python library using the official guide. I've started cloning the minimal sample package suggested in the guide here. I've then added the file my_module.py inside the folder sampleproject storing a simple power function. Another function is also stored in /sampleproject/sampleproject/__init__.py. The resulting structure of the library is the following

enter image description here

Finally, I've used pip to successfully install the package in the interpreter. The only thing left is to make sure that I'm able to run the functions stored in the subfolder sampleproject.

import sampleproject
sampleproject.main()

# Output
"Call your main application code here"

This is great. The package is able to run the function in __init__.py. However, the package is not able to find module.py:

import sampleproject
sampleproject.module

# Output
AttributeError: module 'sampleproject' has no attribute 'module'

I've tried to add __init__.py in the main folder and to change the settings in entry_points in setup.py without success. What should I let sampleproject to be able to find the function in module.py?

Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
Elrond
  • 347
  • 2
  • 15

2 Answers2

1

Your sampleproject.module is a function you would like to execute?

In this case, do as for the sampleproject, add () to execute it: sampleproject.module()

Otherwise, you can import your package like this:

import sampleproject.module

or:

from sampleproject import module

To be clearer, you would have to import module in your sampleproject __init__.py. Then, when you want to use the package, import it (is some py file at root):

import sampleproject  # is enough as it's going to import everything you stated in __init__.py

After that, you can start to use what's in the package you imported with maybe module() if you have a function called module in your package.

init.py discussions

SMFSW
  • 286
  • 1
  • 2
  • 10
  • Your solution work. However, I often read that it could be considered good practice to let __init__.py blank. How do people let users import their modules while keeping __init__.py blank? Does it involve changing the settings in setup.py? – Elrond Aug 28 '17 at 18:19
  • 1
    It's good practice to have comprehensible code too in python. Leaving `__init__.py` blank would require more knowledge of the lib for the user (he has to know about module). It can be fine to leave it blank when making a collection of standalone functions which are not linked together, thus importing only required files (even if there's another way to do that from one huge file: `from lib import fcn1, fcn2...`). Anyways, if you have some init to perform (at module level) like multiple imports, you can do it in `__init__.py` (take a look at the std library in your python install folder for ex). – SMFSW Aug 28 '17 at 19:10
0

it seems,

you are in sampleproject->module.py

so you need to try,

from sampleproject import module
Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118