0

The repo works on my machine but not on gcloud.

Structure of repo (on Google Cloud Source Repo):

project/
├── localdep.py
└── mylocalpackage/
    └── main.py

In main.py:

import localdep

Yet I receive the following error: ModuleNotFoundError: No module named 'localdep'

What am I doing wrong?! There is no problem running this on Pycharm on my machine, yet when I push to gcloud there is...

deckador
  • 55
  • 7
  • I'm not a Python expert by any means but I had a quick study at [The import system](https://docs.python.org/3/reference/import.html). It would appear from the error that your `localdep` is being searched for but not found. Could it be as simple as having localdep.py in the same folder/directory as main.py? – Kolban Dec 09 '19 at 23:29
  • Yes it is possible to do this but in the future I will be adding more localpackages and I need to be able to have this organised into folders. I've also taken a look at the docs and subsequently tried `from ..localdep import *` – deckador Dec 10 '19 at 07:46
  • Have also tried `from ..package import localdep`. When I do this I get `ImportError: attempted relative import with no known parent package`. – deckador Dec 10 '19 at 08:02

1 Answers1

1

The proper structure should be to have the main.py at the top level and the other files on the nested folders. You can take a look into this, which talks about Structuring Your Project.

Just in addition, I have tried with both from ..localdep import * and from ..package import localdep, and other like from ... import localdep, but either I receive ImportError: attempted relative import with no known parent package, or ValueError: attempted relative import beyond top-level package.

It worth to rethink your project structure.

Joss Baron
  • 1,441
  • 10
  • 13
  • Thank you for the link, I will read it now. But the thing is, I want to have multiple Gcloud Functions using this same `localdep`, and I think having them all in the same repo is the ideal way to do this. Do you think there is a better way? – deckador Dec 14 '19 at 14:51
  • If your code has dependencies on that `localdep`, everything will be up to how you structure your code in order to reuse as much code as possible, always keeping in mind your own business logic. [Here](https://cloud.google.com/functions/docs/deploying/repo) you can find the documentation about deploying from source control, just in case you have not taken a look. – Joss Baron Dec 18 '19 at 22:02