21

I'm trying to release my first Python package in the wild and I was successful in setting it up on PyPi and able to do a pip install. When I try to run the package via the command line ($ python etlTest), I receive the following error:

/usr/bin/python: can't find '__main__' module in 'etlTest'

When I run the code directly from my IDE, it works without issue. I am using Python 2.7 and have __init__.py scripts where required. What do I need to do to get this working?

famousgarkin
  • 13,687
  • 5
  • 58
  • 74
OpenDataAlex
  • 1,375
  • 5
  • 19
  • 39
  • 3
    Can you elaborate a bit on what you mean by "run the package via the command line" and "run the code directly"? Can you give an example of the commands that work or don't? – Blckknght Jul 13 '14 at 14:25
  • Sure thing - when I run the package via the command line I'm referring to the package that I built and installed and am running it by using `python etlTest` . When I run the code directly, I'm referring to running the code from within my IDE. – OpenDataAlex Jul 13 '14 at 14:43
  • 1
    Without knowing more of your code, it's hard to guess. – Serge Ballesta Jul 13 '14 at 14:52
  • @OpenDataAlex What's *etlTest*, a Python file? – famousgarkin Jul 13 '14 at 14:55
  • 1
    My code is out on GitHub if you want to take a look: https://github.com/OpenDataAlex/etlTest The main file can be found at etltest/etlTest.py @famousgarkin, It's an open source testing tool that I'm building. – OpenDataAlex Jul 13 '14 at 14:55
  • @OpenDataAlex Yeah, I can see now from the sources, thanks. See below what I found out. – famousgarkin Jul 13 '14 at 15:39

4 Answers4

13

I can easily replicate your problem, actually even without using your package:

$ python empty
.env/bin/python: can't open file 'empty': [Errno 2] No such file or directory
$ mkdir empty
$ python empty
.env/bin/python: can't find '__main__' module in 'empty'
$ python Empty
.env/bin/python: can't find '__main__' module in 'Empty'

So you are not calling your library at all, you are just giving the Python interpreter a nonexistent script name, which in case there is a like-named directory (case-insensitive even) in the working directory it tries to execute it.

I was able to install your package from PyPi just fine and can import it alright, but there doesn't seem to be any entry point to it, nothing useful is exported at the top level and no executable scripts are added to Python bin:

>>> import etltest
>>> dir(etltest)
['__author__', '__builtins__', '__doc__', '__file__', '__name__', '__package__',
'__path__', '__version__']

Running the etlTest.py directly as suggested in Quickstart from your docs doesn't work either:

$ python .env/lib/python2.7/site-packages/etltest/etlTest.py
2014-07-13 17:19:56,831 - settings - DEBUG - Attempting to load .env/lib/python2.7/site-packages/.etltest-settings.yml
2014-07-13 17:19:56,832 - settings - WARNING - No such file or directory .env/lib/python2.7/site-packages/.etltest-settings.yml
2014-07-13 17:19:56,832 - settings - DEBUG - Attempting to load .env/lib/python2.7/site-packages/.etltest-settings.yml
2014-07-13 17:19:56,832 - settings - WARNING - No such file or directory .env/lib/python2.7/site-packages/.etltest-settings.yml
2014-07-13 17:19:56,832 - settings - WARNING - Could not find settings file in .env/lib/python2.7/site-packages/.etltest-settings.yml,.env/lib/python2.7/site-packages/.etltest-settings.yml. Using defaults where present.
Traceback (most recent call last):
  File ".env/lib/python2.7/site-packages/etltest/etlTest.py", line 73, in <module>
    main(sys.argv[1:])
  File ".env/lib/python2.7/site-packages/etltest/etlTest.py", line 22, in main
    SettingsManager().first_run_test()
  File ".env/lib/python2.7/site-packages/etltest/utilities/settings_manager.py", line 29, in __init__
    self.app_name = etltest_config['app_name']
KeyError: 'app_name'

I'd say your package is not apt for ditribution yet. You might want to clean up some dependencies on your development environment and read upon setuptools entry points to provide proper command line executables along with your package. Also it shouldn't run in debug mode by default if it's not some kind of a testing release.

Christian Dean
  • 22,138
  • 7
  • 54
  • 87
famousgarkin
  • 13,687
  • 5
  • 58
  • 74
  • Thank you - this helps immensely! I'll get the package cleaned up and try again. I forgot to change a setting when I packaged etlTest up, which is why it's running in debug mode. – OpenDataAlex Jul 13 '14 at 15:51
13

I recently got the same issue and finaly find the solution by my self. So as the error said, I added a file __main__.py at the same path that my __init__.py.

Inside __main__.py I added the following code :

 from mypackage.mymodule import main 
      main() 

main() was the main function of my code. And it's works now.

here my directory:

package 
|__dirpackage
   |_mypackage.py
   |_ __init__.py
   |_ __main__.py
|_setup.py`
Lbrth_BoC
  • 353
  • 3
  • 7
5

Just change the name __init__.py file to __main__.py

1

I had the same problem and solved it by making sure I'm in the correct directory of the package you are trying to run.

For Windows, type dir in the console, while on Linux/macOS - ls to see your current directory

Valdas
  • 198
  • 1
  • 7