简体   繁体   中英

Including xlrd/xlwt/xlutils with modules outside of python installation

I'm self-taught in the Python world, so some of the structural conventions are still a little hazy to me. However, I've been getting very close to what I want to accomplish, but just ran into a larger problem.

Basically, I have a directory structure like this, which will sit outside of the normal python installation (this is to be distributed to people who should not have to know what a python installation is, but will have the one that comes standard with ArcGIS):

top_directory/
  ArcToolbox.tbx
  scripts/
    ArcGIStool.py (script for the tool in the .tbx)
    pythonmod/
      __init__.py
      general.py          
      xlrd/ (copied from my own python installation)
      xlwt/ (copied from my own python installation)
      xlutils/ (copied from my own python installation)

So, I like this directory structure, because all of the ArcGIStool.py scripts call functions within the pythonmod package (like those within general.py), and all of the general.py functions can call xlrd and xlwt functions with simple "import xlrd" statements. This means that if the user desired, he/she could just move the pythonmod folder to the python site-packages folder, and everything would run fine, even if xlrd/xlwt/xlutils are already installed.

THE PROBLEM: Everything is great, until I try to use xlutils in general.py. Specifically, I need to "from xlutils.copy import copy". However, this sets off a cascade of import errors. One is that xlutils/copy.py uses "from xlutils.filter import process,XLRDReader,XLWTWriter". I solved this by modifying xlutils/copy.py like this:

try:
    from xlutils.filter import process,XLRDReader,XLWTWriter
except ImportError:
    from filter import process,XLRDReader,XLWTWriter

I thought this would work fine for other situations, but there are modules in the xlutils package that need to import xlrd. I tried following this advice, but when I use

try:
    import xlrd
except ImportError:
    import os, sys, imp
    path = os.path.dirname(os.path.dirname(sys.argv[0]))
    xlrd = imp.load_source("pythonmod.xlrd",os.path.join(path,"xlrd","__init__.py"))

I get a new import error: In xlrd/ init .py, the info module is called (from xlrd/info.py), BUT when I use the above code, I get an error saying that the name "info" is not defined.

This leads me to believe that I don't really know what is going on, because I thought that when the init .py file was imported it would run just like normal and look within its containing folder for info.py. This does not seem to be the case, unfortunately.

Thanks for your interest, and any help would be greatly appreciated.

ps I don't want to have to modify the path variables, as I have no idea who will be using this toolset, and permissions are likely to be an issue, etc.

I realized I was using imp.load_source incorrectly. The correct syntax for what I wanted to do should have been:

imp.load_source("xlrd",os.path.join(path,"xlrd","__init__.py"))

In the end though, I ended up rewriting my code to not need xlutils at all, because I continued to have import errors that were causing many more problems than were worth dealing with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM