Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Importing modules error: ImportError: No module named 'openpyxl'

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
1603600f
11198 Views, 7 Replies

Importing modules error: ImportError: No module named 'openpyxl'

from .Modules import openpyxl
File "Modules\openpyxl\__init__.py", line 4, in <module>
from openpyxl.compat.numbers import NUMPY, PANDAS
ImportError: No module named 'openpyxl'

 

After the update on 9 January 2019, I facing this issue. Previously, I am able to run the script without an issue. I am unable to import openpyxl module, even with the Module in the directory.  Some of my other modules like xlrd is fine.

Tags (1)
7 REPLIES 7
Message 2 of 8
JesusFreke
in reply to: 1603600f

Here's one technique I've used before  (note: untested code, somewhat from memory 🙂

 

import inspect
import os
import sys

script_path = os.path.abspath(inspect.getfile(inspect.currentframe()))
script_name = os.path.splitext(os.path.basename(script_path))[0]
script_dir = os.path.dirname(script_path)

sys.path.append(script_dir + "\Modules")
try:
import openpyxl
finally:
del sys.path[-1]

 

Message 3 of 8
1603600f
in reply to: JesusFreke

Hi, I have tried to adding it. I still got back the same error.

 

Here is the full statement of the error:

>>> Traceback (most recent call last):
File "C:/Users/Internet/Desktop/.../.../.../test.py", line 20, in <module>
from .Modules import openpyxl
File "C:/Users/Internet/Desktop/.../.../...\Modules\openpyxl\__init__.py", line 4, in <module>
from openpyxl.compat.numbers import NUMPY, PANDAS
ImportError: No module named 'openpyxl'

Message 4 of 8
JesusFreke
in reply to: 1603600f

My example should allow you to import it by "import openpyxl", not "from .Modules import openpyxl"

 

When reporting things like this, it's helpful if you provide the script you're running, so we can better help you 🙂

Message 5 of 8
1603600f
in reply to: JesusFreke

Great new! Your above method worked. Sorry my bad, I mistakenly forgot taking out my code 'from .Modules import openpyxl'. I just pick up learning pyhton and I would like to understand how your code helped:)

 

I wrote a simple script to test it:

 

 

#Author-Ajay
#Description-

import adsk.core, adsk.fusion, adsk.cam, traceback

import inspect, os, sys

from .Modules import xlrd
print('\nxlrd Module Version: ' + xlrd.__version__)

script_path = os.path.abspath(inspect.getfile(inspect.currentframe()))
script_name = os.path.splitext(os.path.basename(script_path))[0]
script_dir = os.path.dirname(script_path)

sys.path.append(script_dir + "\Modules")
try:
    import openpyxl
finally:
    del sys.path[-1]

print('openpyxl Vodule Version: ' + openpyxl.__version__)

def run(context):
    ui = None
    try:
        app = adsk.core.Application.get()
        ui  = app.userInterface
        #ui.messageBox('Hello script')
        
        print('Done!')

    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

It showed:

xlrd Module Version: 1.2.0
openpyxl Vodule Version: 2.5.12
Done!

 

Thanks for your speedy reply:)

 

Message 6 of 8
JesusFreke
in reply to: 1603600f

So, let me preface this by saying I'm not a python expert, so take it with a grain of salt 🙂

 

It looks like the openpyxl module assumes that it will be imported as a top level module named "openpyxl". When you import it like "from .Modules import openpyxl", that is a relative import, and there is no top level module named "openpyxl", because it's actually a child of the current module. E.g. if the current module was "MyModule", then the name of the openpyxl module would be MyModule.openpyxl.

 

So, when you import openpyxl like "import openpyxl", it searches all of the paths in sys.path for a subdirectory named openpyxl. So you can add the path to the Modules directory to sys.path, which allows python to find the openpyxl module when you import it that way. And importing it that way ensures that there is a top level module names openpyxl, which is what it expects.

 

Also, since fusion 360 has a single instance of python that all the plugins run in, you want to be sure to clean up after yourself and remove any entries to sys.path that you add, so that you have less chance of negatively affecting other plugins.

Message 7 of 8
dtbencomo
in reply to: JesusFreke

Hello, I was wondering if this solution would work for any module import. I have been trying use from 'sklearn import neighbors' https://scikit-learn.org/stable/modules/generated/sklearn.neighbors.KDTree.html 

I even attempted downloading the module file and importing them into my project but still get an error when trying to access using from .sklearn.neighbors import x. 

Any help would be very appreciated. 

Message 8 of 8
niko.quiskamp
in reply to: JesusFreke

Hi folks,

 

I need to reraise this topic again. I tried to follow your advise, but realized that openpyxl has several internal imports which lead to errors while trying to import the library to my script.

Currently I have solved to read excel files by just using XLRD, but as this doesn't support the new xlsx format and there will be no further development, I relly would like to use openpyxl instead.

 

I'm just a python beginner, so does anyone have an advice how to get openpyxl working with a Fusion 360 script or add-in? And is there a way without changing all internal imports of openpyxl?

 

BR

Niko

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report