Potentially Helpful Pointers for OpenSim 4.3 Python 3.8 Install

Provide easy-to-use, extensible software for modeling, simulating, controlling, and analyzing the neuromusculoskeletal system.
User avatar
Aaron Fox
Posts: 289
Joined: Sun Aug 06, 2017 10:54 pm

Potentially Helpful Pointers for OpenSim 4.3 Python 3.8 Install

Post by Aaron Fox » Tue Oct 26, 2021 3:01 pm

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

Tags:

User avatar
Andrea Braschi
Posts: 14
Joined: Thu Mar 11, 2021 2:42 am

Re: Potentially Helpful Pointers for OpenSim 4.3 Python 3.8 Install

Post by Andrea Braschi » Wed Nov 03, 2021 4:00 pm

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

User avatar
Aaron Fox
Posts: 289
Joined: Sun Aug 06, 2017 10:54 pm

Re: Potentially Helpful Pointers for OpenSim 4.3 Python 3.8 Install

Post by Aaron Fox » Wed Nov 03, 2021 7:38 pm

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

User avatar
Andrea Braschi
Posts: 14
Joined: Thu Mar 11, 2021 2:42 am

Re: Potentially Helpful Pointers for OpenSim 4.3 Python 3.8 Install

Post by Andrea Braschi » Thu Nov 04, 2021 1:58 am

Hi Aaron,

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

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

Cheers,

Andrea

User avatar
Ayman Habib
Posts: 2248
Joined: Fri Apr 01, 2005 12:24 pm

Re: Potentially Helpful Pointers for OpenSim 4.3 Python 3.8 Install

Post by Ayman Habib » Thu Nov 04, 2021 2:32 pm

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

User avatar
George Giarmatzis
Posts: 2
Joined: Fri Sep 27, 2019 2:08 pm

Re: Potentially Helpful Pointers for OpenSim 4.3 Python 3.8 Install

Post by George Giarmatzis » Thu Dec 09, 2021 10:42 am

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

User avatar
Andrew LaPre
Posts: 8
Joined: Wed Jun 01, 2022 1:19 pm

Re: Potentially Helpful Pointers for OpenSim 4.3 Python 3.8 Install

Post by Andrew LaPre » Thu Jun 02, 2022 8:50 am

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

User avatar
Ayman Habib
Posts: 2248
Joined: Fri Apr 01, 2005 12:24 pm

Re: Potentially Helpful Pointers for OpenSim 4.3 Python 3.8 Install

Post by Ayman Habib » Thu Jun 02, 2022 1:45 pm

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

User avatar
Andrew LaPre
Posts: 8
Joined: Wed Jun 01, 2022 1:19 pm

Re: Potentially Helpful Pointers for OpenSim 4.3 Python 3.8 Install

Post by Andrew LaPre » Fri Jun 03, 2022 12:34 pm

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

User avatar
Dan Mulla
Posts: 15
Joined: Tue Nov 04, 2014 11:16 am

Re: Potentially Helpful Pointers for OpenSim 4.3 Python 3.8 Install

Post by Dan Mulla » Fri Jun 03, 2022 2:29 pm

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

POST REPLY