Danila Manturov
Danila Manturov

Reputation: 185

Import python package in the module

I am beginner in Python. I am using set python libraries and I want to take the part of my code in .py module. Where should I write "import" of the set of libraries, in the module, or in main file? If I don't write it in the module, program doesn't work.

#mainfile.py
import cv2
import faceResearch
faceResearch.mn()

#faceResearch.py
import cv2
def mn():
    image = cv2.imread("Smiling/3--1873301-Smiling woman looking at camera.jpg")
    cv2.imshow("im", image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
    

so, in which file should I write "import cv2"? in mainfile? in file of the module? or in both?

Upvotes: 0

Views: 52

Answers (1)

Taknok
Taknok

Reputation: 757

You should keep import cv2 in your module (faceResearch.py ?) not in main.py. With that all others scripts importing your module will automatically import cv2 ; if cv2 is installed. You can check if cv2 is installed, and if not display an error message : link

Upvotes: 1

Related Questions