-1

Edit 1: Some context - This is a data science project where main.py kicks off processing and analysis. It is not going to be packaged up.

Edit 2: On why the following is not an acceptable answer: Module not found running on command line The solution is basically, "don't do it". This is a common data science setup. It ought to work.

Edit 3: This is the ideal solution: Create a setup.py file in the root dir:

# project/setup.py

from setuptools import find_packages, setup

setup(
    name='project',
    packages=find_packages(),
    version='0.1.0',
    description='',
    author='me',
    license='',
)

In your requirements.txt add this entry: -e . and run: pip install -r requirements.txt


Original question

My project structure is as follows:

project/
|-- src/
    |-- __init__.py
    |-- main.py
    |-- my_module.py
# my_module
const = 1
# main.py
from src.my_module import const

When I run main.py as:

/project> python src/main.py

I get:

No module named 'src'

Is there a non hacky way around this problem?

GlaceCelery
  • 921
  • 1
  • 13
  • 30
  • Don't run a package module as a script. That's not how it's intended. – 9769953 Sep 26 '22 at 08:17
  • Note: `main.py/` in the structure diagram is listed as an (empty) directory. That's probably not intended. – 9769953 Sep 26 '22 at 08:18
  • If it's not going to be packaged up, why is there an `__init__.py` file? That's confusing. You even have `setup.py` and use it with `pip install`; I call that "packaging up". – 9769953 Sep 26 '22 at 10:08
  • "This is a common data science setup.": no, it's not. Scripts normally go in a separate directory, not in one that looks like a package. – 9769953 Sep 26 '22 at 10:09
  • Kedro and Cookiecutter are on my side: https://kedro.readthedocs.io/en/latest/get_started/example_project.html and https://drivendata.github.io/cookiecutter-data-science/ – GlaceCelery Sep 26 '22 at 10:44
  • 1
    Well, I'll throw in the towel then. But I'll also stay away from those project structures, because at some point, that's going to bite you. (In fact, that already happened in this question.) – 9769953 Sep 26 '22 at 11:48

1 Answers1

1

Since they are in the same folder, you can import it like this from my_module import const

  • Given that `src/` seems to be a package (given the `__init__.py` file), this is the wrong type of import: it mostly resembles a Python 2 relative import, and otherwise it would be an absolute import for a module inside a package, which generally is not going to work. – 9769953 Sep 26 '22 at 08:21
  • @9769953 Since @GlaceCelery asked for a non-hacky solution (i.e., not changing the pythonpath) this is the only one that does not produce an error while running `main.py`, appropriate or not. – Kyriakos Psarakis Sep 26 '22 at 08:31
  • It does, but it is hacky in my opinion, since the package itself (if it's ever used as a package) won't work. – 9769953 Sep 26 '22 at 08:39
  • I agree, but if it was to be used as a package it would have been run externally and not internally by `main.py`. – Kyriakos Psarakis Sep 26 '22 at 08:43
  • I think the OP wants to use both: use `src/` as a package, and run `src/main.py` as a script. – 9769953 Sep 26 '22 at 08:44
  • The OP just said that this is not going to be packaged up. So this is the correct solution. – Kyriakos Psarakis Sep 26 '22 at 09:05
  • This suggestion worked. Thank you – GlaceCelery Sep 26 '22 at 09:12