attempted relative import with no known parent package

Raise code
e TypeError("module name must be str, not {}".format(type(name)))
if level < 0:
raise ValueError("level must be >= 0")
if level > 0:
if not isinstance(package, str):
raise TypeError("__package__ not set to a string")
elif not package:
raise ImportError(
"attempted relative import with no known parent " "package"
)
if not name and level == 0:
raise ValueError("Empty module name")
def _calc___package__(globals):
"""Calcu """
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.
Links to the raise (1)
https://github.com/pytorch/pytorch/blob/e56d3b023818f54553f2dc5d30b6b7aaf6b6a325/torch/package/_importlib.py#L47Ways to fix
This is very easy way to fix this problem is -
Step 1 : creating setup file -
from setuptools import setup, find_packages
setup(name = 'package_name', packages = find_packages())
step 2: Run this - python setup.py install
It will create package into site_packages and make your custom package global.
Get complete detail here
The exception is thrown when importing a module with relative import(start with a dot).
Relative import is used to import a package from the same level where you are.
project/ ├ main.py ├ mylib/ ├ __init__.py │ ├ module1.py │ └ module2.py
For example, you have file structures like this.
inside module1.py
, you could have from . import module2
, because here the .
stands for mylib.
Each file (folder) has its parent. So relative import helps us to call parents of files easily. But when we trying to call a parent model which it doesn't have a parent, we will get an error.
Error code:
from .torch import nn
We can't use the above code, because the torch itself is a top-level parent, it doesn't have a parent package.
Fix version:
from torch import nn
You can check here to get more information about error.