I'm working on a lambda function that partly depends on numpy. I created a deployment package zip with a test script that imports numpy and then tries to subtract two numbers using np.subtract since it was having trouble finding the numpy functions.
import numpy as np
a = np.subtract(4,2)
print(a)
I'm working with a python3.5 virtualenv on a linux EC2 instance. To create the deployment zip, I installed numpy, placed my script into site-packages and zipped the contents of the folder as described here. I can create the lambda function from the zip file with no problem, but when I trigger it, it gives the error:
module initialization error: module 'numpy' has no attribute 'subtract'
It appears to import numpy, but it can't find any of the functions. I assume I packaged the libraries/script wrong, but I thought I was following the directions correctly. Any help would be appreciated!
I think you need to reference the module when you import it. A bit of code always helps.
import numpy
a=2
b=1
c=numpy.subtract(a,b)
print c
For future reference, a similar question was asked here, and I was able to adapt the solution for my needs. It was how I packaged the libraries.
Related
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
Under normal circumstance, external python modules such as scipy and numpy are compiled into shared objects when being installed (The part written in C). When python calls import scipy, it will dynamically load these shared objects.
Now I am working on a platform which does not support any dynamic loading function. As a result, I have to link those modules statically with python.
My current approach is to compile all source code of scipy/numpy with python, and call the module initialization function when python initializes.
Py_initializeEx(){
...
//init scipy modules statically
//below are scipy modules init functions
init_comb();
init_cython_special();
...
}
However, this brings me another problem. I found in many python module initialization functions, especially when they are auto generated from cython, they contain codes to import its parent packages. For example, the cython_special() calls import scipy, but when it is being called, the scipy initialization is not completed yet.
My question is, is there an easy way I can linked these modules statically? What is your suggestions to solve this problem?
Thanks.
PyImport_AppendInittab - this tells Python in advance of a module initialization function associated with a specific name. You'd identify all the modules you need to use that are compiled, link them statically, and then before Py_Initialize you add them to the Inittab.
Nothing happens until the module is imported at runtime when the correct initialization function is run.
If I got you right, what you could do is add a path to a dir where the modules will be located at.
import sys
sys.path.insert(0,'/path/to/modules')
from module1 import *
from module2 import *
etc.
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
I'm trying to build a python script that is suppose to feed into another Matlab program. the script uses (among other things) numpy and pandas.
Here's the matlab code when I try to load the script:
path='C:\XXXXXX\Local\Continuum\anaconda3\python.exe';
pyversion(path)
algo=py.importlib.import_module('Algo_Pres');
When I try to load the script into matlab, I get an import error that seems to originate from python:
I understand the error as: pandas is missing a numpy dependency.
And yet when I turn back to python and run the script in python it works smoothly...
Where do you think the problem comes from?
PS: I checked my Library using conda list in the Prompt.
For some reason numpy is listed in the anaconda channel, whereas anything else is listed without any channel. Do you think it could be linked?
My situation:
need to upload solution .zip file to some server where my solution.py file gets executed. However, I need to use scipy method from package spatial, which is not available on server.
Simple attach whole scipy lib ? - No, I can't beacuse it won't fit in .zip file size regulations, however, if I upload only spatial package from scipy (not using any other) everything would fit (size).
Is it possible ?
Because when pasted simply spatial package into my project folder (files from github repo) it didn't work because of errors like 'Couldn't find foofile' mainly in import instructions.
Thanks, any help appreciated
To see one reason why this does not work, you can do from the (i)python commandline
from scipy import spatial
import inspect
inspect.getsourcelines(spatial)
Under the comment text, you can see several imports. If you also look at the first of those,
inspect.getsourcelines(spatial.kdtree)
there is another import line
import scipy.sparse
This is just the first example I found, there are surely many more dependencies and scipy.spatial won't work as is if you don't have access to them. Note also that numpy is imported, and if your server does not have scipy, it probably does not have numpy either.
My suggestion is to either copy/rewrite the scipy code you need (if this is allowed), write your code without scipy or ask for the server admin to either allow larger files or install scipy.