How to resolve: attempted relative import with no known parent package [duplicate]

2024/9/27 17:30:43

I have a bare bones project structure with mostly empty python files for the sake of testing a concept from an online tutorial:

project|--package1|     |--__init__.py|     |--module1.py||--package2|     |--__init__.py|     |--module2.py||--__init__.py

module1.py:

from .package2.module2 import function2

module2.py:

def function2():return 0

Running module1.py directly results in this error:

Traceback (most recent call last):File "c:\"blahblahblah"\project\package1\module1.py", line 1, in <module>from .package2.module2 import function2
ImportError: attempted relative import with no known parent package

I've tried reducing the complexity of the issue by placing module2.py into the project folder itself and modifying the import as my tutorial suggests it would work (from .module2 import function2) but this yields the same error.

side note: I am under the impression the init files are unnecessary for my version of python, but I've added them to keep all my bases covered.

Python version 3.9.1

Any hints would be much appreciated.

Answer

You can join the path of the outer package inside your source's path for relative module imports:

import os, sys
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))

I guess after this, you can directly do:

from package2.module2 import function2
https://en.xdnf.cn/q/71440.html

Related Q&A

How to create a figure of subplots of grouped bar charts in python

I want to combine multiple grouped bar charts into one figure, as the image below shows. grouped bar charts in a single figure import matplotlib import matplotlib.pyplot as plt import numpy as nplabels…

Python Pillow: Make image progressive before sending to 3rd party server

I have an image that I am uploading using Django Forms, and its available in the variable as InMemoryFile What I want to do is to make it progressive.Code to make an image a progressiveimg = Image.open…

Python - Should one start a new project directly in Python 3.x?

What Python version can you please recommend for a long-term (years) project? Should one use 2.6+ or 3.x is already stable? (only standard libraries are required)UPDATE: according to the answers belo…

Produce random wavefunction

I need to produce a random curve in matplotlib.My x values are from say 1 to 1000 for example. I dont want to generate scattered random y values, I need a smooth curve. Like some kind of very distorted…

How to reference groupby index when using apply, transform, agg - Python Pandas?

To be concrete, say we have two DataFrames:df1:date A 0 12/1/14 3 1 12/1/14 1 2 12/3/14 2 3 12/3/14 3 4 12/3/14 4 5 12/6/14 5df2:B 12/1/14 10 12/2/14 20 12/3/14 10 12/4/14 30 12/5/14 10 …

Google AppEngine Endpoints Error: Fetching service config failed (status code 404)

I am implementing the steps in the Quickstart.I did notice another question on this. I double checked that env_variables section in app.yaml has the right values for ENDPOINTS_SERVICE_NAME and ENDPOIN…

How to unload a .NET assembly reference in IronPython

After loading a reference to an assembly with something like:import clr clr.AddRferenceToFileAndPath(rC:\foo.dll)How can I unload the assembly again?Why would anyone ever want to do this? Because Im …

Bad key axes.prop_cycle Error while using an mplstyle in matplotlib (Python)

I am getting the following error when I try to use an external style sheet loaded locally. Bad key "axes.prop_cycle" on line 270 in idt.mplstyle. You probably need to get an updated matplotli…

Dollar notation in script languages - why? [closed]

Closed. This question is off-topic. It is not currently accepting answers.Want to improve this question? Update the question so its on-topic for Stack Overflow.Closed 12 years ago.Improve this questio…

Failure to build wheel / Error: INCLUDE Environment Variable is empty

I am using Python 2.7.11 and am trying to pip install modules however a few of them are failing. The message I get is "Failure to build wheel for X" and "Error: INCLUDE Environment Varia…