0

I'm building an app to automate some processes that are annoying to do by hand (looking in a file, taking certain information out of a file, building another file with that information).

My project has three main pieces: app.py, which talks to the user and uses webpy to build a simple interface; maker.py, which contains a set of functions that do the heavy lifting; and a folder of html templates. There is also a static folder, which is where the app puts the results of its hard work.

The only way I can get this mess to work is if app.py and maker.py are in the same folder. That doesn't jive with the way various tutorials have told me to set up my folders, though. If they aren't in the same folder, I get errors like "no module named maker". Another problem is that I can't see any good reason for the FragmentMaker class to exist at all. Why can't I have a file full of functions and just import the whole thing as it stands?

What is the best way to set up the folders for this kind of project? Are maker.py and app.py supposed to be in different folders? If so, how do I import maker.py so I can use the code I've put in it?

The web side, app.py :

import web
from maker import FragmentMaker #can't make this work

urls = ('/upload', 'Upload',
        '/make_link', 'Make_Link'
)

app = web.application(urls, globals())
render = web.template.render('templates/')

maker = FragmentMaker() #this doesn't work either


class Upload:
    def GET(self):
        return render.upload_form()


    def POST(self):
        x = web.input(myfile={})
        filename = x['myfile'].filename
        input_file = x['myfile'].file # returns a file-like object
        if '.docx' in filename: #Runs the docx side of fragment maker
            E_supinfo = maker.makeList(input_file)
            maker.WriteFrag(E_supinfo)
        elif '.csv' in filename: #Runs for everything else
            Other_supinfo = maker.ProcessCSV(input_file)
            maker.WriteFrag(Other_supinfo)
        else: #For unsupported file types
            return render.error_page(filename)
        return render.upload_confirm(filename)

class Make_Link:
    def GET(self):
        return render.make_link('NONE')
    def POST(self):
        instring = web.input(accession = 'NONE')
        E_Link = maker.makeLink(instring.accession)
        return render.make_link(E_Link)

if __name__ == "__main__":
app.run()

And this is maker.py, more or less:

import a bunch of useful things other people have built

class FragmentMaker: #does '(object)' need to be in here?
    def __init__(self):
        self.joy = "service is my only joy" #do I need a variable here? Nothing uses this.

    def Thing1(self, stuff_for_thing1):
        useful code here.

    def ProcessCSV(self, other): 
        more code that works

    def makeLink(self, acnum): 
        code here

    def makeList(docname): # does 'self' have to be in these?

        E_List = Thing1(docname) #calling a function from inside another function is ok, right?

        code: then a miracle happens.

... and so on.

my file structure:

makefrag
|-bin
    app.py
|-docs
    empty for now
|-Fragment_Maker
    __init__.py
    maker.py
|-static
    results go here
|-templates
    html files here
|-tests
    all sorts of random test files and code in here
__init__.py
1

I cannot comment so instead I'll answer or lead you there with rhetorical questions:

First, how are you intending on launching as well as using this program? You are building a python package by using __init__.py's but you are executing a module (bin/app.py) within the package as __main__. This is not a typical setup.

Do you plan on importing this package of yours into some other python script? Maybe you don't need a package at all and can use a few scripts. Generally all python files related to some function would be in one cohesive package (perhaps you don't need that top-level __init__.py).

Did you mean for bin/app.py to be executable since it appears that's your only use of it? Add a #!/usr/bin/env python run command to the top of the file, and IMO make the file called app and executable $ chmod +x bin/app.

It would be good to google how python finds modules, how to construct a package, and how env's PYTHONPATH and sys.path work. If your module name doesn't make it to sys.path or is relative to your current directory python won't find it. I added some links below.

If you don't intend on using a python package mechanics you could simply (answer) perform a sys.path.append(os.path.join(os.getcwd(), 'Fragment_Maker')) or sys.path.append(os.path.join(os.dirname(__file__), '..', 'Fragment_Maker')) before your import maker, depending on how you intend on launching your application. Otherwise your question cannot be answered without assumptions from above (e.g. maybe you'll want to put your python script app at top level, keep Fragment_Maker a package, perform a from Fragment_Maker import maker), but assuredly you can now figure it out with these resources:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.