0

So I received three python files with some functions. It's an util library provided by a third party. When I want to import these in my python file I use this code (as was specified by the third party):

import util.submission as S
import util.vis as V
import util.metrics as M

But then I get this error. I am working in Google Colaboraty and I uploaded the three python files in my current working directory on google colab so I don't understand. How do I fix this? The three files are just named submission.py ; vis.py and metrics.py. Any help here? This should be quite basic but it doesn't seem to work.

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-8-1e1451b5bba0> in <module>()
      1 
----> 2 import util.submission as S
      3 import util.vis as V
      4 import util.metrics as M

ModuleNotFoundError: No module named 'util'
CFRedDemon
  • 135
  • 1
  • 7
  • I believe to use `import x.y`, the .py's must be in a folder named `x`, or in this case, `util`. – clubby789 Oct 06 '19 at 12:59
  • Possible duplicate of [ModuleNotFoundError: No module named 'utils'](https://stackoverflow.com/questions/49545142/modulenotfounderror-no-module-named-utils) – thirteen4054 Oct 06 '19 at 13:00
  • `from utils import submission as S` etc. if the files are in a folder called util. If they are in the WD its `import submission. as S` – Alexander Oct 06 '19 at 13:03
  • Please read the following thread [Need help understanding this issue regarding relative vs absolute imports in Python](https://stackoverflow.com/questions/51510407/need-help-understanding-this-issue-regarding-relative-vs-absolute-imports-in-pyt/51511826#51511826) – MichaelR Oct 06 '19 at 14:08

1 Answers1

0

Create a folder in your current working directory named utils, and place the three .py files inside of it. You can then use the import statements as you have specified.

https://realpython.com/absolute-vs-relative-python-imports/#syntax-and-practical-examples

clubby789
  • 2,543
  • 4
  • 16
  • 32