-1

I have a project structure with mostly empty python files:

-project
  --work1
      --__init__.py
      --app.py
      --momo.py

momo.py

import numpy as np
def plus(x):
    return x

app.py

from . import momo
a = momo.plus(6)

Running app.py directly results in this error:

from . import momo
ImportError: attempted relative import with no known parent package

I have tried change to "from plus import momo" but this yields the same error.

Python version 3.8

Any hints would be much appreciated.

Tawan
  • 49
  • 4
  • Does this answer your question? [ImportError: attempted relative import with no known parent package :(](https://stackoverflow.com/questions/63312692/importerror-attempted-relative-import-with-no-known-parent-package) – theProcrastinator Mar 16 '21 at 06:24
  • @YashvanderBamel no, it is different – Tawan Mar 16 '21 at 13:23

2 Answers2

0

You shouldn't import like you said.

you can just try this:

import momo

it should add momo to your current file.

and for using functions which has been declared in momo, you should call the function name after momo.. for example:

a = momo.plus(12)

if you just want to import plus`` from momo``` file, you can try

from momo import plus

then you just need to call function name, instead of the whole file and functions together. e.g :

a = plus(12)
TheFaultInOurStars
  • 3,464
  • 1
  • 8
  • 29
0

Try running it from project directory as module (-m flag):

$ cd project
$ python -m work1.app 

because when you're starting app directly like python app.py, is doesn't know that current folder is actually a package with __init__.py but dot in from . import * is a shortcut for a current package.

madbird
  • 1,326
  • 7
  • 11