#πŸ”’ Python cannot import module in vs code

106 messages Β· Page 1 of 1 (latest)

trim tangle
#

I have an init file in my utils folder so that it can expose the functions inside the utils folder to other python files that im working on. But it seems to cannot detect a function in the utils folder i believe. Why is this happening?

hearty harborBOT
#

@trim tangle

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

trim tangle
polar pond
#

How's the directory structure?

#

all 3 files in same location?

trim tangle
#

video utils and init are in the utils folder

#

and main is just outside

polar pond
#

and read_video is inside video_utils.py?

patent sand
#

Have you saved the file? (I see that dot saying unsaved)

#

If it's not saved to disk, Python cannot find the code, because it doesn't technically exist yet.

trim tangle
patent sand
#

Yup

trim tangle
#

Hmm okay i got out of that error, but now i have a different one

patent sand
#

That's progress πŸ™‚

trim tangle
polar pond
#

video is not loaded probably

trim tangle
#

yeah this is the problems of following just project tutorials

#

i write same to same code, but their understanding doesn't always match that of mine because updated libraries seem to sometimes give some different outputs

polar pond
#

wait, that's the error from save_video()

trim tangle
polar pond
#

based on my experience, 9 out of 10 times yes.

trim tangle
#

py save_video()?

polar pond
#

can you show the main.py and full error message?

#

because if you are following project tutorials, most of the time the function parts are usually correct unless there's a typo

trim tangle
#

this is where im at rn

polar pond
#

it's how you use it

#

hmm let's try debugging together

trim tangle
#

okay

polar pond
#

on line 6 of main.py, try print(video_frames).

#

Pretty old fashioned way I'm using - these days using debuggers are better but welp if it works it works ;p

trim tangle
#

indent it with the function?

polar pond
#

yeap.

trim tangle
polar pond
#

see, you are getting [] as the output, what would that mean?

trim tangle
#

what do you mean by getting [] as output? πŸ€”

polar pond
#

that [] means 2 things

trim tangle
#

oh

polar pond
#
  1. all the lines previous to print(video_frames) did work <-- doesn't mean it worked as you intended, but it did not give error anyways.
#
  1. for some reason, your video_frames are empty even after you read the video using read_video()
trim tangle
#

hmm so i need to somehow fix read_video() and then main.py will work as i intend it to right?

polar pond
#

in your method read_video(), it is said

if not ret:
  break
``` this also means if `read_video()` gets nothing, it will return nothing instead of throwing error.
#

hmm... let's make that function more simple for your understanding

#

but to prevent confusion - no, it's not the problem on read_video()

trim tangle
#

hmm okay

polar pond
#
def function(x):
  list = []
  while x > 1:
    list.append(x)
    x -= 1
    if x == 2:
      break

  return list
#

Now I'm bad at examples, but hope this works

#

!e

def function(x):
  list = []
  while x > 1:
    list.append(x)
    x -= 1
    if x == 2:
      break

  return list

print(function(5))
hearty harborBOT
polar pond
#

So... basically same exact method, but got rid of those cv2 stuffs

#

you see, I've inputted 5, and inside the while loop, the x decrements, until x > 1, right?

#

Tell me anytime if you don't understand.

trim tangle
#

its more like it decrements until x == 2 right?

#

hence there is no 2 in the solution list

polar pond
#

only watching while loop for now

trim tangle
#

hmm yeah i understand the decrement in the loop

polar pond
#

but it is not printing 2 nor 1 because of the break

trim tangle
#

hmm yep

polar pond
#

okay, now back to read_video

#

what is the condition for the break?

trim tangle
#

what does the ret mean again

#

?

polar pond
#

if you read the video through cap.read(), ret will be True. If not, it will be False.

trim tangle
#

and when its false, the loop will break right?

polar pond
#

exactly

#

so what you can try is

#

add print("error loading video") before the break to see if it was triggered.

trim tangle
#

what's the problem with that though? shouldn't it still read through all of the frames in the video until the frames run out?

polar pond
#

the problem is... it's not reading anything currently

#

and I suspect the reason to be you not having input_videos directory inside the project

trim tangle
#

i don't think it got triggered

polar pond
trim tangle
#

so you mean theres no video in the video_path directory?

polar pond
#

yes, that's what I think.

trim tangle
polar pond
#

but now you see the text printing

trim tangle
#

i dont think i see "error loading video"? πŸ€”

polar pond
#

right before the []

trim tangle
#

oh wait

#

yes okay

#

so that means its encountering the break right?

polar pond
#

indeed yes

trim tangle
#

so this is indeed just a video location error?

polar pond
#

that's what I thought when I first saw the screenshot at least

polar pond
# trim tangle

because this photo didn't show any video folders - maybe cropped

#

but as I said

#

9 out of 10 times, video loading error

trim tangle
#

i just copied the code off the tutorial but im not sure where that video_path parameter refers to

polar pond
#

in your actual project folder

trim tangle
polar pond
#

you must have a folder named input_videos, and inside it, you must have a video file named 08fd33_4.avi

trim tangle
#

how did you figure that?

polar pond
#

first off - I just watched this error too many times

trim tangle
#

i basically named the input_video folder as test data folder

#

let me rename it

#

and it has that file

polar pond
#

second, I saw at the terminal that save_video() was being called

#

which means all precedented codes are 'syntax-wise' okay

#

thirdly, it said index out of range - which means it's reading something beyond one's limit

#

but in the save_video() you were simply using cv2.VideoWriter, which has no bug, so it has to do with video file itself.

#

fourth, you said you were following tutorial, which means the error will lie on the environment settings, not the actual code.

hearty harborBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.