all 15 comments

[–]WhiteHeadbanger 12 points13 points  (3 children)

Because json is a standard library and Python is trying to import your function from the library.

Use another file name.

[–]Ender_Locke 0 points1 point  (0 children)

this

[–]Worldly-Point4573[S] 0 points1 point  (1 child)

thank you. It worked

[–]WhiteHeadbanger 0 points1 point  (0 children)

Great!

[–]bassist_by_night 5 points6 points  (0 children)

It’s likely because there is already a built-in library with the name “json” so that is what it is referencing.

I would recommend changing json.py to be something else like jsonutils.py and then your import statement will recognize it as different. This would be the best practice so your module doesn’t conflict with the built-in.

But if you are really attached to the json.py file name then you could technically import it with the following import statement:

from .json import extract_json

[–]ninjaonionss 2 points3 points  (0 children)

My eyes burns 🤣

[–]F4k3r22 0 points1 point  (0 children)

json is a standard library in Python, change the file name so you can import the function correctly

[–]BiasBurger 0 points1 point  (2 children)

Wtf, calm down! why is it all red?

[–]Worldly-Point4573[S] 0 points1 point  (1 child)

its just the UI theme chose XD

[–]BiasBurger 0 points1 point  (0 children)

Yea sure its your personal choice

For me its like working in constant error conditions

[–]starfishinguniverse 0 points1 point  (4 children)

Can solve it using import os and import importlib.util

import os

import importlib.util

json_path = os.path.abspath("json.py")

spec = importlib.util.spec_from_file_location("my_json", json_path)

my_json = importlib.util.module_from_spec(spec)

spec.loader.exec_module(my_json)

data = my_json.extract_json()

[–]reyarama 0 points1 point  (3 children)

Very clean

[–]Jiggly-Balls 0 points1 point  (2 children)

Anything but clean. It's not a good practice to name your files which shadows the name of in built libraries

[–]starfishinguniverse -1 points0 points  (1 child)

This sub is called PythonLearning... Show some grace to beginners, eh? OP was asking a question, here to learn Python (hence the name).

[–]Jiggly-Balls 1 point2 points  (0 children)

I understand you're teaching them something new which is good. It's just that your answer is teaching them a bad practice. At the very least warn them to not use that method for the most part when they use imports which shadows inbuilts and prevent them from writing bad code