I am working on developing a python package to simplify SOAP communications to Cisco Unified Communications Manager's AXL API. CUCM does not allow access of the WSDL directly via URL, instead, the WSDL must be downloaded locally. Instead of having to download the WSDL on each machine that I install my package on, I want include it as part of my package. The WSDL from CUCM comes in a folder called axlsqltoolkit
. To test functionality, I created a little module called axl_connection.py
with a simple function:
import os
def wsdl():
os.startfile(os.path.join(os.getcwd(), 'axlsqltoolkit'))
Then, downloaded the WSDL and placed it in the same directory as: axl_connection.py
. I created a simple test_axl_connection.py
script(in a separate directory):
import from axl_connection import wsdl
print('Testing the opening of the WSDL directory')
wsdl()
The problem with this code is that my script tries to open axlsqltoolkit
from the same directory as test_axl_connection.py
. I need for it to open it from the same directory as my imported module instead. How do I achieve this?
Additionally, is it wise to place supplementary files such as my WSDL inside of my python package?
don't use os.getcwd()
determine the path of your module by looking at __file__
and in test_axl_connection.py
import os
MYPATH = os.path.realpath(os.path.dirname(__file__))
def wsdl():
os.startfile(os.path.join(MYPATH, 'axlsqltoolkit'))
__file__
is a magic variable containing the path of the file containing the source of the module.
os.path.dirname(__file__)
gives you the directory name of that file. As this could be a relative path and you never know whether somebody changes the current working directory it's a good idea to convert it immediately (during import) into an absolute path.
However I personally create normally a separate directory for non python files, so you might do something like:
import os
MYPATH = os.path.join(
os.path.realpath(os.path.dirname(__file__)), "data")
def wsdl():
os.startfile(os.path.join(MYPATH, 'axlsqltoolkit'))
and place the wsdl file in a directory named data located in the directory, that contains test_axl_connection.py