Setting up folders and importing modules in Python

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

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:

  • https://docs.python.org/2/tutorial/modules.html#packages
  • https://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH
  • https://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python
  • https://stackoverflow.com/questions/50499/in-python-how-do-i-get-the-path-and-name-of-the-file-that-is-currently-executin
  • https://stackoverflow.com/questions/11536764/attempted-relative-import-in-non-package-even-with-init-py

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Setting up folders and importing modules in Python

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

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:

  • https://docs.python.org/2/tutorial/modules.html#packages
  • https://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH
  • https://stackoverflow.com/questions/72852/how-to-do-relative-imports-in-python
  • https://stackoverflow.com/questions/50499/in-python-how-do-i-get-the-path-and-name-of-the-file-that-is-currently-executin
  • https://stackoverflow.com/questions/11536764/attempted-relative-import-in-non-package-even-with-init-py

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật