Page 1 of 2

Potentially Helpful Pointers for OpenSim 4.3 Python 3.8 Install

Posted: Tue Oct 26, 2021 3:01 pm
by aafox
Hi All,

I recently updated to OpenSim 4.3 and had some interesting experiences with respect to getting the module to work within a Python environment. I'm not sure the below issues occurred with any consistency (as I went through a few different install attempts), and that these tips are 100% solid - but maybe my experience here as a reference could help iron out any similar issues others have.

For reference, this was installing OpenSim 4.3 on Windows, into a virtual Anaconda environment with Python 3.8.
  • The instructions on the OpenSim Python install webpage obviously need to be followed, but I also believe these need to be followed in the exact order they are state in (i.e. add the dll directory via os FIRST, and then run the setup scripts.
  • I think there is also a need to add the Simbody \bin as a dll directory as well, similar to what is instructed with OpenSim. Without doing this, I ran into an error with importing the opensim module, with this error relating to not being able to import things associated with Simbody.
  • Once you get opensim installed and working, if you are using Spyder I found that installing the spyder package via different channels produced an interesting difference. I typically launch spyder from my environments via the Anaconda Prompt (i.e. command). Previously I've installed spyder from the conda-forge channel, and when doing this the output log from OpenSim (e.g. the optimisation steps from a Moco problem) gets displayed in the Anaconda command window (quite useful I think). I noticed this behaviour disappeared when I installed spyder using pip in recent environments, so this may be a consideration for Python users.
Like I said, I'm not 100% sure of some of these things but this was my experience - so hopefully it can help someone out and de-stress some aspects of installing updated software!

Aaron

Re: Potentially Helpful Pointers for OpenSim 4.3 Python 3.8 Install

Posted: Wed Nov 03, 2021 4:00 pm
by andreabraschi
Hi Aaron,

I am replying to your post because I believe you may be preciously helpful for my struggle of importing the opensim library into Python 3.8. I am new to Python, therefore that's where my struggle may come from.

Anyways:
>>> import os
>>> os.add_dll_directory("C:/OpenSim 4.2/bin")

These first two commands are supposed generate a new ddl directory, right? However, the instruction given by OpenSim specify that "for version 4.3+ you need to change directory to the sdk/Python folder under the install folder". How did you change the directory to the sdk/Python folder under the install folder?

Can these commands be given in PyCharm so that a new DDL is added within the virtual environment we're working in?

Many thanks in advance for your time.

Andrea

Re: Potentially Helpful Pointers for OpenSim 4.3 Python 3.8 Install

Posted: Wed Nov 03, 2021 7:38 pm
by aafox
Hi Andrea,

My understanding is that the need to be in the sdk/Python folder is when running the install commands via the command prompt/powershell - not in python where those os commands are done. So I guess my steps would be: - jump into a python terminal and run the os commands to add the 'bin' directory; - jump into a command/powershell console, navigate to the sdk/Python folder using cd, and then run the install commands.

Make sense?

Aaron

Re: Potentially Helpful Pointers for OpenSim 4.3 Python 3.8 Install

Posted: Thu Nov 04, 2021 1:58 am
by andreabraschi
Hi Aaron,

yes, it does make sense. Apparently, I got it working.

Many thanks for your time. I really appreciate your help.

Cheers,

Andrea

Re: Potentially Helpful Pointers for OpenSim 4.3 Python 3.8 Install

Posted: Thu Nov 04, 2021 2:32 pm
by aymanh
Thanks much Aaron.

I'll just add that the requirement to call add_dll_directory() was introduced by python 3.8 (and later) on windows only so if using earlier version of python or if not on windows you can ignore this step.

All the best,
-Ayman

Re: Potentially Helpful Pointers for OpenSim 4.3 Python 3.8 Install

Posted: Thu Dec 09, 2021 10:42 am
by ggiarmatzis
Hi all,

I have followed the exact same above guidelines to install Opensim 4.3 API with python 3.8 (using anaconda/spyder), however when I am trying to import opensim, i get the error
"ImportError: DLL load failed while importing _common: The specified procedure could not be found."

Does anyone have a hint about what's wrong here?

regards
Giorgos

Re: Potentially Helpful Pointers for OpenSim 4.3 Python 3.8 Install

Posted: Thu Jun 02, 2022 8:50 am
by andrewklapre
Hi all,

It's been a while since I've used the OpenSim API and first time with Python, though I use Python pretty frequently. I'm currently using Python 3.9, OpenSim 4.3, and PyCharm for my IDE.

After following the instructions for configuring OpenSim to use the Python API in Windows, everything appears to install correctly, but when calling "import opensim" in the python terminal, I get the error: "ImportError: DLL load failed while importing _simbody: The specified module could not be found." If I add the Simbody/bin folder with the os command, it doesn't seem to change anything. I've added both OpenSim/bin and Simbody/bin to the Path variable as well but that also doesn't seem to make a difference. The IDE shows both OpenSim and Simbody bin folders in the External Library tree but also returns "ModuleNotFoundError: No module names 'opensim'" when running "import opensim".

Does anyone have any idea what I am doing wrong?

Thanks in advance!
-Andy

Re: Potentially Helpful Pointers for OpenSim 4.3 Python 3.8 Install

Posted: Thu Jun 02, 2022 1:45 pm
by aymanh
Hi Andy,

On windows platform Python3.8 changed how dll's are located, primarily the PATH environment variable is not used to locate dlls any more. We work around that by executing a separate script only on windows that invokes the line

Code: Select all

os.add_dll_directory("actual_dll_path")
You can try to execute this line in your environment before importing and let us know how it goes.

Moving forward we'll publish conda packages that do not rely on the Path or GUI installation to work around this issue.

Best regards,
-Ayman

Re: Potentially Helpful Pointers for OpenSim 4.3 Python 3.8 Install

Posted: Fri Jun 03, 2022 12:34 pm
by andrewklapre
Hello Ayman,

Yes, I tried this but it still does not recognize OpenSim. From a normal python terminal if I do this it then doesn't find simbody. I'm not sure where the hiccup is but I was able to get a Matlab license to write my optimizations. If I can figure out what I'm doing wrong I'll post to this thread in case anyone else wants to use OpenSim in PyCharm.

Thank you,
-Andy

Re: Potentially Helpful Pointers for OpenSim 4.3 Python 3.8 Install

Posted: Fri Jun 03, 2022 2:29 pm
by mulladm
Hi Andy,

I think your error could likely be due to using Python 3.9. I've been able to get it set up for Python 3.8, but the same steps don't work with Python 3.9 or 3.10. Here's step-by-step proces of what I did to get it working using OpenSim 4.3 and Python 3.8 in case if it helps anyone (using the tips outlined by Aaron above):

1. Create a virtual environment in Python setup with Python 3.8. If you have Anaconda, this can be done by opening the Anaconda prompt window and typing:

Code: Select all

conda create -n nameofenv python=3.8 anaconda
Or alternatively using PyCharm, you could create a new project using Python version 3.8 in the options. If going this latter route, you will need to install numpy.

2. Assuming you are using an anaconda prompt, then activate the environment and call python. This can be done by typing into your anaconda prompt window:

Code: Select all

conda activate nameofenv
python
3. Once you're within python, you will need to add the libraries. You will need to adjust to the path based on where your installed files are, but for me this was:

Code: Select all

import os
os.add_dll_directory("C:/OpenSim 4.3/bin")
os.add_dll_directory("C:/OpenSim 4.3/sdk/Simbody")
4. Exit out of python:

Code: Select all

exit()
5. Navigate to the OpenSim setup file path (this path could vary based on where your files are installed). Run the setup file:

Code: Select all

chdir C:/OpenSim 4.3/sdk/Python
setup_win_python38.py
6. Hopefully that should do the trick and trying to "import opensim" in python should be fine within this environment.

Best of luck!

Dan