0

I have a folder structure :

setup_seplot.py
seplot/
        __init__.py      (empty)
        seplot.py
        kw_dictionaries.py

In seplot.py, I have :

import kw_dictionaries as kd

If I run seplot.py, everything works well.

However, there is a problem when I use setup_seplot.py :

python setup_seplot.py sdist bdist_wheel
Traceback (most recent call last):
  File "setup_seplot.py", line 2, in <module>
    from seplot import seplot as sep
  File "/home/XXXXX/code/Python-Tools/seplot/seplot.py", line 14, in <module>
    import kw_dictionaries as kd
ModuleNotFoundError: No module named 'kw_dictionaries'

This issue seems to come from the fact that in setup_seplot, I import seplot to get the version :

setup_seplot.py :

from setuptools import setup, Extension, find_packages
from seplot import seplot as sep

version=sep.__VERSION__
setup(
     name='seplot',
     version=version,
     description="A front-end for Python PyX",
     install_requires=[ 'pyx', ],
     packages=find_packages(),
     scripts=['seplot/bin/seplot','seplot/seplot.py',
        'seplot/kw_dictionaries.py','seplot/style_dictionaries.py']
 )

If in seplot.py I replace

import kw_dictionaries as kd

by :

from . import kw_dictionaries as kd

Then the setup works fine, but the code (setup.py) doesn't. I am quite lost here.

petezurich
  • 9,280
  • 9
  • 43
  • 57
SergeD
  • 44
  • 9

2 Answers2

0
  • Keep your scripts and packages separated by using the old-style scripts from distutils. Or even better, use the console_scripts entry points from setuptools.
  • Always use absolute imports in the executable scripts (if you have any).
  • Use absolute or relative imports in the packages as you prefer (I believe absolute is better in the long run).
  • In the setup script, don't import the packages from the project. Keep the version string in setup.cfg (or setup.py) and use importlib_metadata.version('ProjectName') to read from within your actual code.
sinoroc
  • 18,409
  • 2
  • 39
  • 70
  • Thanks, but what would this look like in practice ??? I tried keeping kw_dictionaries.py in a separate folder (inside `seplot/` ) but I ran into the exact same problem ! – SergeD Jan 16 '20 at 12:19
  • I guess the real problem with any organization is that I import a package from the project in the setup script. But I need this to pass the version to the setup file. What would be the clean way to pass the version otherwise ? – SergeD Jan 16 '20 at 12:25
  • For the version string, use the suggestion 5 of : https://packaging.python.org/guides/single-sourcing-package-version/ -- Even better, use `importlib_metadata.version('ProjectName') https://docs.python.org/3/library/importlib.metadata.html#distribution-versions – sinoroc Jan 16 '20 at 12:42
  • @SergeD still having difficulties with this or is the question answered? – sinoroc Jan 23 '20 at 16:14
  • I think it is solved, thank you ! I will test more with a more minimal (basically) project to make sure I fully get it. – SergeD Jan 24 '20 at 10:28
-1

The solution was found in : another topic as suggested by Gocht.

It feels hacky and I don't seem to understand why, but this works :

if __package__:
    import seplot.kw_dictionaries as kd
else:
    import kw_dictionaries as kd
SergeD
  • 44
  • 9