gts2_client.py - double import trap
Hi there,
When installing the gts2_client via "python setup.py install" it installs the library once as a python package and additionally as a command line scripts due to the line scripts=['gts2_client/gts2_client.py'] in setup.py. When the installation is finished I have files with the names gts2_client.py in the following 3 locations:
- conda_environment/bin/gts2_client.py
- conda_environment/lib/python3.7/site-packages/gts2_client-0.5-py3.7.egg/gts2_client/gts2_client.py
- conda_environment/lib/python3.7/site-packages/gts2_client-0.5-py3.7.egg/EGG-INFO/scripts/gts2_client.py
The problem is that the script(s) has the same filename as the module does. The import from a python interpreter works as expected:
from gts2_client import gts2_client # works ok
I have an own module that uses gts2_client and has some tests defined. Pytest while executing the tests somewhere down the stack calls import via ‘from gts2_client import gts2_client’. In this sitiuation however, python seems to try to import the gts2_client.py module under the scripts or bin folders, which is a toplevel module (no init.py file in the same folder) and therefore has to be imported as "import gts2_client" without specifying any package. So my code runs differently in production and test environments, which is not desirable.
Wouldn’t it be better to have a special script file called ‘gts2_client’ without the extension ‘.py’ that imports and lets you execute the gts2_client? That way python wouldn't get confused about which module to import.
The following site has additional information about the problem, as it seems to be haunting not only the gts2_client :) http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html#the-double-import-trap
all the best and cooler weather
Niklas