6

The structure of my_dir is

├── README.md
├── main
│   ├── functions
│   │   ├── __pycache__
│   │   ├── my_function.py
│   ├── pipeline.py
│   ├── options
│   │   └── pipeline_options.py
│   └── transforms
│       ├── __pycache__
│       ├── my_transform.py
├── poetry.lock
├── pyproject.toml
└── tests

On pipeline.py:

from main.functions.my_function import MyFunction

On my_function.py:

import apache_beam as beam

class MyFunction(beam.DoFn):
...

I have read similar questions here, including this one which is the solution I have currently. Also read this on Python imports.

When I run pipeline.py, I get

Traceback (most recent call last):
  File "pipeline.py", line 7, in <module>
    from main.functions.my_function import MyFunction
ModuleNotFoundError: No module named 'main'

Additionally, I'm using VSCode and if I run the file via "Python: Run Python File in Terminal" I get back the error. However, if I run via the debugger the paths are all found and the error is not thrown, which I found odd. Also, VSCode isn't throwing any path warnings.

I know there are similar questions but I haven't been able to figure out what is wrong here and have spent quite some time on this already. Any help/pointer is much appreciated.

Additional info

  • I'm using python 3.8.1
  • Using poetry run python main/pipeline.py to run the code
  • Running from command from my_dir
mcansado
  • 2,026
  • 4
  • 25
  • 39
  • 1
    Have you tried adding `__init__.py` files to your directories to indicate they should be treated as packages? – dfundako Sep 02 '20 at 14:11
  • in pipeline you should directly call ```from functions.my_function import MyFunction``` – Abdeslem SMAHI Sep 02 '20 at 14:12
  • I had before but I'm using python 3.8.1 and `__init__` files are only needed for Python <3.5 – mcansado Sep 02 '20 at 14:12
  • I have tried that too, it returns the same error and is also flagged by VSCode (`unresolved import 'functions.my_function'Python(unresolved-import)`) – mcansado Sep 02 '20 at 14:14
  • @mcansado Are you running your Python file from the directory where main directory is located? Or are you running it from main directory? – wprazuch Sep 02 '20 at 14:21
  • Running it from where main is located (meaning if I run `ls`, `main` is in the results) – mcansado Sep 02 '20 at 14:24
  • Have you tried to do in your pipeline.py script before the imports statments : `os.sys.path.append('../main/functions')` and after that you only do `from my_function import MyFunction` – Xeyes Sep 02 '20 at 14:27

0 Answers0