Stan Kurilin
Stan Kurilin

Reputation: 15792

Imports in distributable python scripts

I have a python project pypypy with 2 files: __main__.py and foo.py. In __main__.py I simply do import via import foo. It all works fine.

Now, I want to distribute it with pypi. After installing my module I'm execution it with python -m pypypy. When I do that, the import statement doesn't work anymore. However import pypypy.foo does the job.

Should I change all my imports before distribution or there is a better way?

Upvotes: 1

Views: 99

Answers (1)

hurlenko
hurlenko

Reputation: 1425

Using absolute imports is strongly suggested as they work consistently across different python versions. Check this answer. In your case you should prefer using import pypypy.foo.

The reason it works in your dev environment might be because of PYTHONPATH manipulation. For example Pycharm automatically sets Add content and source roots to PYTHONPATH. Also when you run python it automatically adds current working directory to PYTHONPATH.

Upvotes: 1

Related Questions