I'm having trouble calling a function I've written after importing the module containing it. The module is called funky.py. Here is the code for funky.py:

from ftplib import FTP_TLS

####    FUNCTION  ---  fileup()   #### for sending file via FTP TLS ##
## arg1 ..  destname > the filename to be created on the destination machine
## arg2 .. sourcepath > the absolute path to the source file
## usage .... fileup('foobar.txt', 'C:/dir1/dir2/orig_foobar.txt', '123.45.678.90', 'myusernm', 'mypassword')
## Note 1. Will overwrite on destination if file already exists
## 

def fileup(destfile, sourcepath, host, uname, password ):
    # open FTP connection
    ftps = FTP_TLS(host,user=uname, passwd=password)
    ftps.prot_p()                                   # Invokes protected mode
    ftps.set_pasv(False)                            # Avoids timeouts
    outbound = 'STOR ' + destfile
    with open(sourcepath, 'rb') as filein:
           ftps.storbinary(outbound, filein, 8192)
    ftps.close()


####    FUNCTION  ---  doyr()   #### generates day of year (1-366)##
## usage .... doyr()  no arguments
## returns  3 digit julian date (day of year)
##
import datetime 
def doyr():
    jd = datetime.date.today().strftime("%j")
    return(jd)


print(doyr())

It lives in the directory 'C:\Python33\My_Own_Stuff' which is included in my PYTHONPATH environmental variable (OS is Windows Server 8, R2). If I import it straight from the shell, it works fine:

>>> import sys
>>> sys.path
['C:\\Python33\\Lib\\idlelib', 'C:\\Python33\\My_Own_Stuff', 'C:\\Windows\\system32\\python33.zip', 'C:\\Python33\\DLLs', 'C:\\Python33\\lib', 'C:\\Python33', 'C:\\Python33\\lib\\site-packages']
>>> import funky
329
>>> from funky import doyr
>>> doyr()
'329'

However, if I run another script, located in 'C:\Users\user101\Documents\101_Scripts', when I try and use the 2nd function 'doyr' in the script I get an "AttributeError: 'module' object has no attribute 'doyr'" Then if I try the same shell sequence as above I get an
" ImportError: cannot import name doyr".

>>> import sys
>>> sys.path
['C:\\Users\\user101\\Documents\\101_Scripts', 'C:\\Python33\\Lib\\idlelib', 'C:\\Python33\\My_Own_Stuff', 'C:\\Windows\\system32\\python33.zip', 'C:\\Python33\\DLLs', 'C:\\Python33\\lib', 'C:\\Python33', 'C:\\Python33\\lib\\site-packages']
>>> import funky
>>> from funky import doyr
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    from funky import doyr
ImportError: cannot import name doyr
>>> 

So why does the 2nd function, 'doyr' work in the shell but error when imported to the other script, or in the shell immediately afterward? The only thing that changes is the current working directory, but the funky module is in 'C:\Python33\My_Own_Stuff' which is in the search path in both instances. Can't figure this one out.

Thanks

I'm embarassed to admit this, but maybe it will help someone else from doing the same stupid thing. I started out writing funky.py in the 101scripts directory. Then decided I should have a separate directory for my own reusable functions so created My_Own_Stuff directory, called it out in PYTHONPATH and continued to work on it. Problem was, I left the original copy in the 101Scripts directory so it never got updated. So when 101Scripts was the working directory, my scripts were looking at the old funky.py module which didn't have the doyr function in it (but did have fileup). Lesson learned. Pay attention to your housekeeping!!

commented: Nice sharing mind! +12

Useful tips:

import funky
print(funky)
print(funky.__file__)

This prints the path to the file where the module was found. Very useful when a module behaves in an strange way.

great tip. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.