0

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?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Danila Manturov
  • 185
  • 1
  • 2
  • 8

1 Answers1

1

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

Taknok
  • 717
  • 7
  • 16
  • Thank you) I Should I use special file for imports and add in in each module? Because I will have much of libraries, not only cv2. – Danila Manturov Aug 07 '17 at 16:09
  • To summarize : each time you wrote `cv2` in a file, put `import cv2` in this file. – Taknok Aug 07 '17 at 16:13
  • @ДанилаМантуров if this solves your issue, pls pass the post as resolved, else ask for precision. – Taknok Aug 07 '17 at 19:21