1

I've written a small python code that parses data from a file and then uses Selenium to input the data into a website, and I am trying to bundle it with a script.

I was already able to bundle it into a pip package and upload it, but when trying to run it I get ImportError: No module named '<name>' for modules on the same directory.

My structure is pretty simply

chessil_tourney_inserter/
    setup.py
    chessil_tourney_inserter/
        __init__.py (empty)
        chessil_tourney_inserter.py
        swiss98_text_parser.py
        command_line.py

And the setup.py is also pretty basic:

from setuptools import setup

setup(name='chessil_tourney_inserter',
    .
    .
    .
    packages=['chessil_tourney_inserter'],
    zip_safe=False,

    install_requires = [
       'selenium'
    ],

    entry_points={
        'console_scripts': [
        'insertchessiltourney = chessil_tourney_inserter.command_line:main']
    })

As of right now command_line.main simply calls chessil_tourney_inserter:

import chessil_tourney_inserter.chessil_tourney_inserter as cti
import sys

def main():
    if len(sys.argv) == 1:
        print("Usage: chessil_tourney_inserter.py *tournament name*")
        exit()
    cti.main();

if __name__ == "__main__":
    main()

and chessil_tourney_inserter gives me an import error on:

import swiss98_text_parser

but if I try to run chessil_tourney_inserter.py directly it works, and if I add the package name to the import it would break chessil_tourney_inserter.py

So how am I supposed to set up the files so that imports will work correctly both when I run the file directly myself, and when I try to import it as a package or run it as a script?

Nescio
  • 513
  • 4
  • 13

1 Answers1

1

Add __init__.py at the top folder too.

chessil_tourney_inserter/
setup.py
__init__.py
chessil_tourney_inserter/
    __init__.py (empty)
    chessil_tourney_inserter.py
    swiss98_text_parser.py
    command_line.py

See What is __init__.py for? for more information.

Community
  • 1
  • 1
Pramod
  • 5,150
  • 3
  • 45
  • 46
  • So simple and works perfectly. Thank you! I read on the __init__.py, but didn't realize it should be outside the directory... On my searches I found somewhere that starting from python 3 if the package name matches a directory it would search inside it even without the __init__.py file, is that the reason the code worked with the package name even without it? – Nescio Sep 04 '15 at 14:05
  • Yes, I think so. [Pep 420](https://docs.python.org/3/whatsnew/3.3.html#pep-420-implicit-namespace-packages) introduced this change. Which version of Python are you using? – Pramod Sep 04 '15 at 14:09
  • I'm using version 3.4.2 currently – Nescio Sep 04 '15 at 14:11
  • `__init__.py ` is not need for namespace packages. `Namespace` packages are those which only contain other packages. Basically, they contain folders, but not files. You have two packages with the same name. You wanted the top level folder `chessil_tourney_inserter` to also be a package. Since it has a file `setup.py`, it is a `regular` package and hence needs an `__init__.py` Also see http://stackoverflow.com/a/30324656/817277 TL;DR `__init__.py` not required for `namespace` packages, but required for `regular` packages – Pramod Sep 04 '15 at 17:32