Home › Forum › SOFA › Using SOFA › [SOLVED] How to import numpy in python scripting of SOFA?
Tagged: 64_bits, Linux_ubuntu, SOFA_1706
- This topic has 11 replies, 5 voices, and was last updated 4 years, 7 months ago by Zahra.
-
AuthorPosts
-
3 July 2019 at 15:34 #13900ZahraBlocked
Hi,
I have a question regarding numpy library in SOFA. It seems that SOFA have created a SOFAnumpy library. IS this true? So when I want to import numpy in my code, should I import “SofaPython.SofaNumpy”? I am asking this because if I just import numpy in my code, I receive this import error:
“from . import _distributor_init
ImportError: cannot import name _distributor_init”To my knowledge, this happens because of two numpy library installation, one from the Anaconda and one from SOFA, however, I couldn’t solve this issue.
Does anybody know which version of numpy SOFA is using?Thanks in advance for any guidance.
Zahra
4 July 2019 at 17:34 #13905Bruno MarquesBlockedHi Zahra,
Sofa does not package numpy with its sources, so your problem, while indeed being an issue with concurrent versions of numpy on your system, is not linked to your install of SOFA. (as far as I know)
I’m no expert in this, but I’d guess that:
– you installed numpy either via your package manager (apt install python-numpy) or via pip (pip install numpy)
– then installed Anaconda, which does, of course, redistributes its own version of python.I *think* that you might be able to fix your issue by uninstalling numpy’s pip (or aptitude) package (using pip uninstall or apt remove).
———————
It seems that you dug a bit in the SofaPython plugin before posting, that’s nice 😉
There’s indeed an example called SofaNumpy in SofaPython, which uses a piece of python code that imports numpy. But this script relies on your system’s install of numpy, so as said before, it should not be the cause of your problem.
That being said, it seems that the SofaNumpy code is supposed to provide shared-memory pointer access to Sofa’s data fields through a numpy array abstraction, which is a must-have feature IMHO. The code does not seem to be functional though, sadly.I actually never realized that there was a SofaNumpy module present in SofaPython, it’s a pretty cool discovery for me. If you have the skills and feel like restoring this code, feel free to do so, we’ll be more than happy to see such a pull request in SOFA!
Meanwhile, we are working on a similar approach in the next gen SofaPython3 plugin whose beta version should be released soon… *teaser*Anyway, I hope this answers your question, and best of luck!
10 July 2019 at 18:08 #13927ZahraBlockedDear @Bruno Marques,
Thank you for your kind reply, and sorry for the late reply, my code were running so I couldn’t try your hint and give you the feedback sooner.
I tried your hints, I uninstalled everything, including Anaconda and numpy, until there was no numpy there. I then reinstalled Anaconda, and the problem persists. At least it’s helpful to know that it has nothing to do with SOFA libraries. May I ask if you are using Anaconda for python scripting?
Thanks for your explanation on SofaNumpy, and on your news about SofaPython3. Would SofaPython3 plugin have this feature that we can import sofa module directly in Spyder?
Regarding restoring the mentioned code, thank you for encouraging me. I would like to contribute SOFA and challenge myself in the pull requesting, but restoring this code doesn’t motivate me that much. I also didn’t feel that confident at this moment for it.
Best regards,
Zahra
3 August 2019 at 20:31 #14057Damien MarchalBlockedHi @Zahra
On my side I don’t use Anaconda and prefer to rely on pip as much as I can.
About SofaPython3. The answer is yes, it make it possible to import Sofa modules and run a simulation directly in any python interpreter (Jupyter, Spyder, python3) in addition to the embedded one in runSofa.Regards,
Damien.24 August 2019 at 15:35 #14154ZahraBlockedDear @Damien,
Thank you for your explanations on SofaPython3. It looks great!Best,
Zahra24 August 2019 at 15:42 #14155ZahraBlockedMy problem with importing numpy has solved. In my case, the problem was that the default python in my system wasn’t the one that SOFA is using. Changing the default python removed the error.
Best,
Zahra30 March 2020 at 10:24 #15580SimonMemberHi @Zahra,
I am having the same problem as you had. Good to see you have solved that.
– What do you mean by the default python? I have Anaconda2, Anaconda3 and a clean python 2.7, which one is the default? How do I find out?
– How can I find out which python my SOFA is using? I am so far using the precompiled SOFA binaries.
– How to change the default python? Has this something to do with building SOFA or is this some system setting. I am using Windows 10.
Thanks in advance
Simon30 March 2020 at 13:20 #15582jnbrunetModeratorHey @simon ,
Can you create a very simple python scene file with the following:
info.py
import sys def createScene(root): print(sys.prefix) print(sys.executable) print(sys.path)
Then run it with runSofa and give us the output?
30 March 2020 at 14:46 #15583SimonMemberHi @jnbrunet,
thanks for the fast reply, in the meantime I have installed python 2.7 and pip installed numpy into that.
sys.prefix
is the one I needed to check.Output in the runSofa console window:
[INFO] [SceneCheckerVisitor] Finished validating node “root”.
C:\Python27 C:\Users\swtouch\SOFA\v19.12.00\bin\runSofa.exe ['', 'C:/Users/swtouch/SOFA/v19.12.00/plugins/SofaPython/lib/python2.7/site-packages', 'C:\\WINDOWS\\SYSTEM32\\python27.zip', 'C:\\Users\\swtouch\\Anaconda2\\Lib', 'C:\\Users\\swtouch\\Anaconda2\\DLLs', 'C:\\Python27\\Lib', 'C:\\Python27\\DLLs', 'C:\\Python27\\Lib\\lib-tk', 'C:\\Users\\swtouch\\SOFA\\v19.12.00', 'C:\\Users\\swtouch\\SOFA\\v19.12.00\\bin', 'C:\\Python27', 'C:\\Python27\\lib\\site-packages'] [INFO] [SceneCheckerVisitor] Validating node "root" with checks: [SceneCheckAPIChange, SceneCheckDuplicatedName, SceneCheckMissingRequiredPlugin, SceneCheckUsingAlias]
Thanks
Simon31 March 2020 at 11:35 #15595ZahraBlockedHi @Simon,
Regarding the problem I encountered at that time, I solved it with so much trial and error. This is my final understanding of the process:
When I simply imported numpy in anaconda or in the Terminal without using SOFA, I had no problem. But when I imported SOFA and numpy, there was a conflict between different versions of numpy (although there seemed to be just one version of it). Uninstalling and reinstalling numpy didn’t work for me. So I guessed that there is a conflict here between numpy versions in the version of python that SOFA is using, and the default python that my system is using. Changing the default python of my system from python 2.7 to python 3.7 solved the issue.1- I also had Anaconda2, Anaconda3 and a clean python 2.7 on my system (Linux Ubuntu) that I encountered this problem. To find out which python is the default of the system, in the Linux Terminal simply type python (I guess “command prompt” in Windows is working similar to terminal in linux).
This is the result I get which shows the default python of my system is now Python 3.7.6:
$ python
Python 3.7.6 (default, Jan 8 2020, 19:59:22)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type “help”, “copyright”, “credits” or “license” for more information.
>>>You can also try this:
In terminal (or in command prompt of windows) type:
which pythonThis gives you the folder of the default python:
/home/zahra/anaconda3/bin/python2- I didn’t deal with this directly.
3- I do not exactly remember the command for changing default python of the system, but there are some commands in the web (I’m not sure but I think it was this: alias python=python3). In my experience, it had nothing to do with building SOFA (I remember I rebuilt it but that didn’t solve the issue).
Hope this helps,
Zahra31 March 2020 at 18:31 #15610SimonMember31 March 2020 at 22:48 #15615ZahraBlockedNice to hear that your problem is solved, and thanks for your explanation.
-
AuthorPosts
- You must be logged in to reply to this topic.