1

I'm attempting to release a command line program written in Python to the public, but would first like to test it to make sure that the program works as I intend. To do so, I create a simple wheel distribution of the program, create a virtualenv instance in another directory, and install the wheel into said environment. Easy, right?

Not so.

When I attempt to call the program in the testing environment, I receive this error message:

Traceback (most recent call last):
  File "~/testing/bin/leprechaun", line 7, in <module>
    from leprechaun.main import main
  File "~/testing/lib/python3.4/site-packages/leprechaun/main.py", line 8, in <module>
    import generator
ImportError: No module named 'generator'

So, it seems that the generator.py file isn't being imported for some reason. What's strange, however, is that generator.py and main.py (the main entry point to the program) exist in the same directory.

For reference, here's my project's tree hierarchy:

├── leprechaun
│   ├── data
│   │   └── A bunch of text files that aren't important
│   ├── db.py
│   ├── generator.py
│   ├── __init__.py
│   ├── main.py
│   └── rainbow.py
├── LICENSE.txt
├── MANIFEST.in
├── README.rst
└── setup.py

And, for kicks, my setup.py script:

#!/usr/bin/env python3

import re
from setuptools import setup, find_packages

version = re.search("^__version__\s*=\s*\"(.*)\"$",
  open("leprechaun/__init__.py").read(), re.M).group(1)

setup(
  name='leprechaun',
  version=version,
  description="A simple rainbow table generator",
  long_description=open("README.rst", encoding="utf-8").read(),
  author="My Name",
  author_email="My Email",
  url="https://github.com/zcdziura/leprechaun",
  license="MIT",
  packages=find_packages(),
  package_data={
    "leprechaun": ["data/wordlist*"]
  },
  entry_points={
    "console_scripts": [
      "leprechaun = leprechaun.main:main"
    ]
  }
)

I assumed that the entry-point script that is generated would simply call the main function from my package, which would then be able to import modules from the same directory. However, that doesn't seem to be the case.

How would I fix this?

1 Answer 1

1

The problem you're facing deals with absolute imports. You might want to take a look at this PEP for more information on what the rationale behind it is.

The short answer is that every import statement you make now looks along sys.path. Whereas in Python 2.x, you could use import generator and it would look in the current directory, Python 3.x requires it to be along the path. So if you change to from leprechaun import generator or from . import generator you should be able to clear this up.

Sign up to request clarification or add additional context in comments.

1 Comment

That did it! I didn't realize that Python 3 changed the import behavior. I guess it makes sense why they did it, though. Now I simply have to fix a couple of new bugs that have sprung up. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.