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?

5
  • Please post some code that you have tried. It's unclear exactly what your question is asking Commented Aug 7, 2017 at 15:49
  • Please add your code as an example Commented Aug 7, 2017 at 15:54
  • Thank You, I've done it. Commented Aug 7, 2017 at 16:01
  • Why do you want to separate mainfile.py and faceResearch.py? Both are technically modules, and if all you want to do is execute the code in faceResearch.py you should get rid of mainfile.py and run faceResearch.py directly. Commented Aug 7, 2017 at 16:06
  • It is only excample, I will have classes and methods in faceResearch.py Commented Aug 7, 2017 at 16:11

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
To summarize : each time you wrote cv2 in a file, put import cv2 in this file.
@ДанилаМантуров if this solves your issue, pls pass the post as resolved, else ask for precision.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.