Search code examples
pythonmodulepython-importsetuptoolssetup.py

Using Custom Modules in Python


I have the following package/directory structure:

PythonMDK>>
    README.txt
    setup.py
    PythonMDK>>
        code1.py
        code2.py
        __init__.py

And the setup.py file contains the following:

from setuptools import setup
setup(
    name='PythonMDK',
    version='1.0dev',
    description='Python MDK',
    author='ME',
    author_email='[email protected]',
    packages=['PythonMDK'],
    long_description=open('README.txt').read(),
    url='')

How do I now install this and use the classes/functions contained within "code1.py" and "code2.py"? I tried python setup.py install and it seemed to work, but now I have a "dist" and "build" folder in the main "PythonMDK" folder and I can seem to use any of the contained functions. So basically what do I have to do now that I have the "setup.py" file in order to make use of those modules?


Solution

  • For import PythonMDK to expose any of your functions in code1.py, the file PythonMDK/__init__.py must import them like from code1 import myfunction. Then you could do import PythonMDK; PythonMDK.myfunction().

    See https://docs.python.org/3/tutorial/modules.html#packages, https://docs.python.org/3/reference/import.html#packages, and https://docs.python.org/3/reference/import.html#regular-packages