How can I import module?

How can I import module?

edyczkowski
Community Visitor Community Visitor
1,417 Views
1 Reply
Message 1 of 2

How can I import module?

edyczkowski
Community Visitor
Community Visitor

Hi 

 

I have my python already installed in root. I'm looking to use pySerial in Fusion 360 and I have "ImportError:" I already tried http://help.autodesk.com/view/fusion360/ENU/?guid=GUID-743C88FB-CA3F-44B0-B0B9-FCC378D0D782 and then "from .modules import serial" but it doesn't work

 

Obviously, when I do pip it goes to my python in root.

 

I'm bit lost with 2 python environments.

 

Could you please advise?

 

Thanks

0 Likes
1,418 Views
1 Reply
Reply (1)
Message 2 of 2

KrisKaplan
Autodesk
Autodesk

Ideally, addins and scripts should self contained.  Any non-standard package/module dependencies should be located in the script's folder (or sub tree) and be imported using relative imports ('from . import foo').  This is no problem when you author the dependent packages and modules yourself, or if they are themselves designed to be self contained.  But this is more complicated for packages and modules that were not designed to be self contained, and instead make assumptions about their dependencies (or themselves) being in the system path.  And unfortunately, this appears to be the case for pySerial.

 

When I tried to relative import the 'serial' package after installing it in a relative path, the debugger emits errors on the 'import' statement with a message similar to "ImportError: No module named 'serial'" with the top of the call stack pointing to a line in one of the serial module files.  The problem is that in several places throughout the serial package, there are imports in the form 'from serial.xxx import yyy'.  The code in the serial module assumes that the 'serial' packages is in the system path.  You can either 'fix' the code in the serial package and change all the absolute imports to relative imports (e.g. change 'from serial.xxx' to 'from .xxx'), or add path to your script's 'modules' folder to the sys.path before the import statements.  (Modifying the system path should be avoided if possible, because it could impact other scripts or Fusion if it causes module name collisions.)

 

BTW: When installing a dependent library with pip, you would want to make sure that your current python version is as close as possible to the version that Fusion is using (which is currently version 3.5.3).  And you should use the '--target' argument with pip to install the package in your script's module folder (instead of the system library path of your current running Python version).

 

Kris



Kris Kaplan