python module that imports another python module - python

I'm new to python and I'm having an issue importing a module that imports numpy,PIL and os packages. I'll try and be as clear as possible with my problem
So I have a module lets call it preprocessing.py in which I've written a class to process an image imported from PIL using Image and converting it to a numpy array so the structure looks like the following (note method1 converts a jpg to numpy array)
----- preprocessing.py
import numpy as np
import os
from PIL import Image
Class process_object:
method1
Now I want to use this module as follows I want to import process_object from preprocessing.py and use method1 to process an image again imported using Image in PIL. So my script computation.py looks like the following
---computation.py
import os
import numpy as np
from PIL import Image
a = process_image(input)
a.method1()
However, when I do this I get the following error message
ImportError: No module named numpy
Could someone explain to me what is going on and how to fix it? I'd really appreciate an explanation which allows me to understand what is going on under the hood, so I can avoid situations like this. I really appreciate any help! Thanks!!

Check in which version of Python pip is installing numpy. It could be that when pip installs it, it's pointing to a different Python version on your system.
For problems like these, I would recommend using:
https://github.com/pyenv/pyenv-virtualenv
Will handle Python versions for you, so that you can differentiate which packages are being installed.
I will also recommend using PyCharm's Community Edition.
https://www.jetbrains.com/pycharm/download
Excellent tool and lets you create your own environment.
Hope this helps.

https://sourceforge.net/projects/numpy/files//NumPy/1.5.0/NOTES.txt/view. This is the support for numpy in Python 3.0. You probably need a newer version of numpy. You can also use:
pip install numpy
or
pip3 install numpy

Related

Why scipy.integrate shows has no "simpson" attribute?

I have some python code which uses function scipy.integrate.simpson
So for example I import scipy using import scipy.integrate as scp_int and then use it in following way:
vol_r = scp_int.simpson(f_integrand_r,dx=_dx1,axis=0)
I get this error
vol_r = scp_int.simpson(f_integrand_r,dx=_dx1,axis=0)
AttributeError: module 'scipy.integrate' has no attribute 'simpson'`
I have made sure that I have scipy package installed, using pip install scipy, and I have restarted computer. I don't know why is this happening? Can someone give the reason or help me solve this issue?
I suspect you are using version 1.5 (or an older version) of SciPy.
If you don't know which version you have installed, you can check with
import scipy
print(scipy.__version__)
simpson was added in 1.6.0; it is the new name for the old function called simps. If you can't upgrade, you should be able to use simps instead of simpson.

How to install project in git repository correctly in python?

Im newbie in python and know i a task from my lecturer to use ANFIS. so I intall anfis in my virtual environent using
pip install anfis
and now I try to import that
import anfis
import membership #import membershipfunction, mfDerivs
import numpy
i got problem
ModuleNotFoundError: No module named 'membership'
so what I must do?
Looks like membership is part of the anfis package, but you are trying to import it as if it were an independent package in itself. Since you haven't installed any package called membership (probably because it doesn't exist as a package), you are getting a ModuleError.
Solution:
First of all, remove the following statement from your code, which is causing the error message:
import membership
Next, since you are already importing the entire anfis package, you can just use a function from the membership module in your code like this:
import anfis
# some code
mfc = membership.membershipfunction.MemFuncs(mf)
Check this link from the anfis GitHub repo for an example on how to use the membership function.
This seems to be a common problem. Try reading through this discussion and see if anything there helps you.
https://github.com/twmeggs/anfis/issues/4

Importing OpenCV - import cv2 or cv3, and why does the directory matter?

I am trying to use OpenCV for Python 3 for the first time. To install, I simply typed "pip3 install opencv-python" into the terminal. When I do this, I see in Finder (I'm on a Mac), that in /usr/local/lib/python3.7/site-packages, I now have a new folder called "cv2". This is strange. Why isn't it cv3, as I used pip3? I do not know.
When I try to import cv2 from python3, it says "No module named 'cv2'". Luckily StackOverflow already has a solution: if I first change directories like: "cd /usr/local/lib/python3.7/site-packages" then I will be able to open cv2. I do not know why this step is necessary for OpenCV. I don't have to do this to "import numpy" for example; "numpy" does not care what directory I start from, and it is listed like just another module like cv2.
Anyways, I change directory to site-packages, start python3, and I import cv2. Now there is a new error. This time cv2 is found, but it says:
"from .cv2 import *
ModuleNotFoundError: No module named 'cv2.cv2'"
For the record, I also have Python 2.7. If I try "cd /usr/local/lib/python2.7/site-packages" and then start Python 2.7, I can import cv2. This is ok, but I want to use OpenCV with Python3.
Let me summarize.
(1) How can I get cv2 or cv3 to import when I start from any directory? Is this ever possible?
(2) How can I get cv3 for python3? cv2 seems to be problematic in python3 because because there is no "cv2.cv2"
The only thing we need to know here is, that 2 in cv2 is not a version. cv2 uses API of C++ while cv uses C API. Version of it is constantly updating being inside CV2.
There is no cv3 there's only import cv2 that opencv older or latest that is updated for old python or latest.

difference in using the import and from * import statement

When I install PIL http://www.pythonware.com/products/pil/ on 32 bit python, I can use import Image and then use the Image library inside PIL. However, for the 64 bit python, I downloaded http://www.lfd.uci.edu/~gohlke/pythonlibs/. However, when I use import Image, it gives me error, "No Module named Image". However, I can use from PIL import Image.
Why is that I can use import Image directly in one instance and not in another?
You want to install Pillow instead.
It comes in both 32 and 64 bit variants for Windows, and has fixed long-standing issues with the original PIL library.
The PIL library has long been broken when it comes to packaging. Pillow was started as a fork to fix these issues once and for all, and version 2.0.0 added Python 3 support (with a grant from the Python Software Foundation).
They're different libraries. Or, to be specific, they're different packaging of the same library.
PIL is notorious for these sort of issues. Just use Pillow instead.

Difference between "import cv" and "import opencv.cv" using Python + OpenCV?

I'm trying to use OpenCV with Python and converting some C++ code. Anyway, if I do:
import cv
img = cv.LoadImage('image.jpg')
It's ok. Or:
import opencv.cv as opcv
size = opcv.cvSize(40, 50)
But anyway, the cv module doesn't have the cvSize data structure and the opencv.cv doesn't have the LoadImage. So, what exactly does each module have? I tried looking in the documentation but couldn't find it. Am I supposed to use it like this or is my setup misconfigured?
The real answer is :) that both "import opencv.cv" or "from opencv import cv" are the old-style wrapping imports.
Since OpenCV 2.0, the new-style Python wrapping is used, and the style you should use looks like this:
# import only cv, no opencv
# this also brings in sub modules such as highgui
import cv
# no "cv" prepended before all method names
src_mat = cv.LoadImageM('yourfilename.png', cv.CV_LOAD_IMAGE_GRAYSCALE)
# let's show the image in a window
cv.NamedWindow('your name', 1)
cv.ShowImage('your name', src_mat)
cv.WaitKey
The old-style wrappings made use of SWIG, the new-style wrappings, judging by the opencv 2.2 source code, seem to be self-made.
With:
import cv
You're importing the cv module from wherever it exists in the python modules search directories. This could be a different version of the module stored somewhere outside the opencv package install as it appears to be in this case.
But with:
import opencv.cv
You're explicitly importing the opencv packages version of cv, i.e. the one in the install directory for the opencv package. This version almost guarantees you have the one from the opencv package and it seems to be the same as using the syntax:
from opencv import cv

Categories