0

I've got a local project structure that looks something like this:

local_root/
├─ reusable_code/
│  ├─ helpers.py
├─ main/
│  ├─ script.py

Inside of script.py, I have the statement from reusable_code import helpers. When I'm working from my local tree (which happens to be in a parallel directory to the actual project), this works just fine and there are no issues.

However, when I push to the 'live' repo, which of course has a structure identical to this, script.py always fails to run because it "can't find any module 'reusable_code'". I've tried running the live version of script.py from the command line and from task scheduler, and even tried using the python executable located in my local project's venv to run the live version of script.py, but it still won't run.

I've also tried appending the absolute path to the reusable_code directory using sys.path.append(<path>/reusable_code), but that didn't work either. Any further suggestions on how to go about fixing this?

Thanks to all in advance!

  • When you do `sys.path.append` you need to provide the parent directory of `reusable_code` so it can be found. – Tim Aug 23 '22 at 18:40
  • 1
    @Tim That makes sense, I had tried appending the path to reusable_code and then importing helpers.py directly and that didn't, and I'm not sure why? After reading your comment though I appended the parent to path and did my import as "from reusable_code import helpers" and it worked, so thanks for that! Now I understand why that *did* work, I'm not sure why it wouldn't work previously? Could it be because I was running my local copy from within an IDE with build-in tools to search the project directory, while running on a CLI only looks from the cwd? – confused_nomad Aug 23 '22 at 18:57
  • 1
    yes, modern IDEs does too good a job of finding local scripts in a project that it spoils us even when the import isn't correct. When importing `helpers.py`, this doesn't work because you are missing a `__init__.py` in the `resusable_code` folder. Adding that empty file makes `resusable_code` into a package so that it can find scripts under it when you do import. – Tim Aug 23 '22 at 19:07

0 Answers0