0

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?

6
  • Don't run a package module as a script. That's not how it's intended. Commented Sep 26, 2022 at 8:17
  • Note: main.py/ in the structure diagram is listed as an (empty) directory. That's probably not intended. Commented Sep 26, 2022 at 8: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". Commented Sep 26, 2022 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. Commented Sep 26, 2022 at 10:09
  • 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.) Commented Sep 26, 2022 at 11:48

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

7 Comments

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 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.
It does, but it is hacky in my opinion, since the package itself (if it's ever used as a package) won't work.
I agree, but if it was to be used as a package it would have been run externally and not internally by main.py.
I think the OP wants to use both: use src/ as a package, and run src/main.py as a script.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.