1

I'm trying to set up a proper Python package for distribution and I'm running into problems with the console_scripts property of my setup.py file. I have the following in my setup:

<...>
entry_points={"console_scripts": ["solve_cipher=cipher_solver.solve_cipher:main"]}
<...>

I have packaged and sent to the test PyPI and then installed my packaged in a new virtual environment. Now, the command solve_cipher is available but running it gives me:

$ solve_cipher 
Traceback (most recent call last):
  File "/Users/markus/.virtualenvs/cipher_solver_debug_pypi/bin/solve_cipher", line 6, in <module>
    from cipher_solver.solve_cipher import main
ModuleNotFoundError: No module named 'cipher_solver'

However, if I just start Python and run the same import line, it works just fine:

$ python
>>> from cipher_solver.solve_cipher import main
>>>

Why does this happen? My project layout is as follows:

cipher_solver
    <...>
    cipher_solver
        consts.py
        simple.py
        solve_cipher.py
        utils.py
    <...>
    setup.py
7
  • which python? head -1 /Users/markus/.virtualenvs/cipher_solver_debug_pypi/bin/solve_cipher? Commented May 20, 2020 at 20:50
  • @phd #!/Users/markus/.virtualenvs/cipher_solver_debug_pypi/bin/python3.7 Commented May 21, 2020 at 8:32
  • Looks good. Is it the same as which python? When you import cipher_solver from the command line what is the current directory? The one where setup.py resides? Commented May 21, 2020 at 9:09
  • Thanks! I solved the issue, see answer below. Commented May 21, 2020 at 9:22
  • @phd Hmm, I think I spoke too soon about the answer. Yes, they are the same Python binary, and I run it from where setup.py is. Any other ideas? Commented May 21, 2020 at 10:00

1 Answer 1

1

I found the issue. I used setuptools.find_packages() in my setup.py and it turns out that method needs __init__.py files in packages and subpackages for them to be found. I thought this was no longer necessary in Python 3.3+ but apparently in some cases.

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

4 Comments

You might want to give find_namespace_packages a try. Dependeing on your exact requirements tt might be helpful.
@sinoroc I don't have any namespace packages.
as far as I know, it shouldn't matter if you have namespace packages or not. Maybe the name of the function is a bit misleading, not sure exactly. Anyway, last time I tried this function was able to collect packages that do not contain any __init__.py package initializer. But to be honest, I still prefer putting __init__.py in all my packages and use the normal find_packages function. I just thought I would mention this alternative.
Thank you, appreciate it. I'm fine with the __init__.py files, as long as I can get it to work :)

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.