Python: Failed to connect to Motor-CAD instance

Dear forum,
we have been using Motor-CAD 2024.1.3 with Python scripts in the last months.
On the same PC (Windows) Motor-CAD 2024.2.3.1 has now been installed. This seems to lead to some unexpected behavior: when opening the same models and scripts in the same older 2024.1.3 version, the Python scripts suddenly fail at the second line:
Python script Exception: An error occurred in user script.
User Script, line 6, in
mcApp = pymotorcad.MotorCAD()
^^^^^^^^^^^^^^^^^^^^^
Exception: Failed to connect to Motor-CAD instance: port=58016, Url=http://localhost:58016/jsonrpc
We also tried to open everything in the new 2024.2.3.1 but the behavior is the same.
We also checked the menu "Defaults -> Automation" and tried different “registered” versions. Nothing worked.
As soon as we switch to another PC, where Motor-CAD 2024.2.3.1 has not been installed yet, everything works as before. So we are pretty sure that the new Motor-CAD installation causes this and not any change in our environment.
Is there any workaround to make the scripts running again on all machines?
Thank you!
Answers
-
Hi @mathias thanks for your post. Hopefully @PyMotorCAD-Team will be able to help you!
0 -
Hi @mathias thanks for your message. There should be no compatibility issues with the Motor-CAD v2024.2.3 installation and PyMotorCAD. I have a few questions:
1. Are you using a virtual environment?
2. If so, which Python version are you using?
3. If not using a virtual environment, can you please go to the Sripting -> Settings tab in Motor-CAD and click Display Version and report which version of PyMotorCAD is installed?Thanks. The answers to the questions above should help us to determine what issue might be occurring here.
0 -
Hello!
Thanks for answering. We have no virtual environment.
The requested output is: "Python script output: PyMotorCAD version: 0.5.5"However, we have tried it on another PC meanwhile. The same issue occurs there as well, as soon as we install v2024.2.3, Python scripts stop working in all installed MotorCAD versions.
On this second machine I have updated PyMotorCAD to the latest release to give it a try. The output there now is: "Python script output: PyMotorCAD version: 0.6.3"Nothing has helped yet.
0 -
Hi @mathias , it's difficult to understand what is causing this issue. Have you tried uninstalling Motor-CAD v2024.2.3 to see whether everything returns to how it was before?
I recommend:
- Uninstalling Motor-CAD v2024.2.3
Open Motor-CAD v2024.1.3 and try the script
If everything works again, we can try to reinstall Motor-CAD v2024.2.3
Test whether the problem reappears
If the problem persists after uninstalling Motor-CAD v2024.2.3 we need to figure out what the problem is.
0 -
Hi @mathias & @PyMotorCAD-Team,
Did we get any resolution on this issue?
A customer is facing the same issue at my end as well.
Thanks.
0 -
Hi @mathias and @PyMotorCAD-Team
My customer also face same error.
How did you resolve this issue?The customer's Motor-CAD version:24.1.3, Python: 3.11
0 -
Hello @Anil_Vaghela and @Shinsuke Okayasu
No resolution on this topic yet, unfortunately. I was not able to un- and re-install MotorCad several times due to running projects. However, the proposed steps from @PyMotorCAD-Team seem not to solve any problem but mainly should narrow down (or even just confirm) the issue. At least the confirmation has been done by you now, so thanks for your posts!
1 -
Hi @mathias
Thank you for your reply.
My customer also tried un- and re-install Motor-CAD. but it is not resolved.
And tried below item. these trial also can't be resolved.
・allow specific port number which fixed by Motor-CAD by WindowsOS
・Intrinsic port number of "mcApp = pymotorcad.MotorCAD(port=xxxxxxx)"I suggest to change PC which can execute script in Motor-CAD.
I have not found a solution yet.Are there any more suggest for finding solution?
0 -
Hello @Shinsuke Okayasu,
please be aware that installing on another PC will potentially also introduce the problem there. We already have two machines that are no longer able to run Python scripts in MotorCad before we stopped rolling out the newest MotorCad versions on all machines.0 -
The customer clicked the "Update to Latest Release" button in Motor-CAD to update pymotorcad, but encountered a proxy error as shown in the attached figure.
Is this proxy error related to the port error mentioned earlier?0 -
Hi @Shinsuke Okayasu, I suspect the problems may be related. When updating to the latest release, Motor-CAD is calling pip with 'python.exe -m pip install --force-reinstall ansys-motorcad-core'. From the error message, it looks like pip can't access the internet via their proxy.
For the update problem, one option would be to manually download the 'wheel' file from https://pypi.org/project/ansys-motorcad-core/#files, and install this (making sure it goes into the right Python installation, so if they have a default installation, the command would be something like this
- c:\Ansys_Motor-CAD\2024_1_3\Python\Python\python.exe -m pip install ansys_motorcad_core-0.7.0-py3-none-any.whl
(Here, make sure you are using the path to the right Motor-CAD version, and the .whl file is downloaded and available in the current folder)
However, it is quite likely that if PyMotorCAD can't access Motor-CAD because of their networking configuration, the original problem will remain. I'd hope that this is something the customer's IT support could help with, if they know that Motor-CAD and PyMotorCAD need to communicate locally over RPC. We'll look into this further to check why this problem
0 -
@Shinsuke Okayasu One thing to try, in case 'localhost' is being routed via a proxy, is we could try using the 'localhost' IP address directly.
Could you ask the customer to open Motor-CAD, check the port currently in use, and run this script from inside Motor-CAD? The user will need to edit port_number to match what is being used in their Motor-CAD session:
- # Try connecting to PyMotorCAD directly via IP address, in case localhost is being routed via a proxy
- import ansys.motorcad.core as pymotorcad
- # Find this port name from Defaults->Automation, "Current Port:"
- port_number = '56190'
- # Connect to Motor-CAD
- try:
- mcApp = pymotorcad.MotorCAD(url='http://127.0.0.1:' + port_number)
- print("Connected via IPv4")
- except:
- try:
- mcApp = pymotorcad.MotorCAD(url='http://[::1]:' + port_number)
- print("Connected via IPv6")
- except:
- print("Failed to connect")
0