1

I have a python package called 'my_package'. The structure is as follows

my_package
  my_package
    __init__.py
    __main__.py
    my_package.py
    some_other_file.py
    run_script
  setup.py

'run_script' is just a shell script that calls the package as a module using the virtual environment that is specified in the script, roughly:

#!/bin/bash
/path/to/myenv/bin/python -m my_package

'my_package' is obviously installed into myenv beforehand.

The problem is this: When the script is called, despite '-m' flag is specified, python still calls 'my_package.py' instead of importing 'my_package' from the environment.

Is there a way to override this? Somehow make it so that python doesn't use my_package.py file?

My solution would be just to rename my_package.py file. But I would hope to find something better.

Ray P.
  • 875
  • 1
  • 9
  • 24
  • Doesn't running `$ python -m my_package` run `my_package/__main__.py`? Why is it running `my_package.py` anwyay? – FHTMitchell May 15 '18 at 12:14
  • Just tried it, a package called `my_package` with files `__init__.py, __main__.py, my_package.py` called with the `-m` flag only enters `__main__.py`, not `my_package.py`. Could you be a bit more specific about exactly `__main__` and `my_package` do? – FHTMitchell May 15 '18 at 12:18
  • I was expecting it to run my_package/__main__.py, but apparently the fact that run_script and my_package.py are located in the same directory makes it run my_package.py, because it encounters it first. The key to recreating this issue is to run the script from the directory where my_package.py is located. – Ray P. May 15 '18 at 12:20
  • Ahh I see. Can you call `__main__` directly? Or put the `__main__` logic in another file which main calls? Or, as you said, rename `my_package.py`? – FHTMitchell May 15 '18 at 12:22
  • Because I need relative imports in the package to work correctly (and I have experienced that with just calling __main__ they don't, only when the package is imported with python -m). It's also described here: https://stackoverflow.com/a/22250157/7082015 – Ray P. May 15 '18 at 12:28

0 Answers0