简体   繁体   中英

Import different functions from another file in Python?

I have a main file in python that I am hosting through Google App Engine, however, I have all my functions defined in the same class. This is obviously not as clean and not as useful for file management. How can I have all my functions in a different file and then import that file to use the functions?

Here is my file that is a basic date validator:

import webapp2

def valid_year(year):
    if (year.isdigit()):
        year = int(year)
        if (year < 2030 and year > 1899):
            return True

def valid_day(day):
    if (day.isdigit()):
        day = int(day)
        if (day <= 31 and day > 0):
            return True

def valid_month(month):
    months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
                        'September', 'October', 'November', 'December']
    month_abbvs = dict((m[:3].lower(), m) for m in months)
        if month:
            short_month = month[:3].lower()
            return month_abbvs.get(short_month)

form="""
<form method = "post">

    <label>Month</label>
    <input type = "text" name = "month">

    <label>Day</label>
    <input type = "text" name = "day">

    <label>Year</label>
    <input type = "text" name = "year"><br>

    <input type="submit" value="Validate Date">
</form>
"""

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.write(form)

    def post(self):
        Month_Test = valid_month(self.request.get('month'))
        Day_Test = valid_day(self.request.get('day'))
        Year_Test = valid_year(self.request.get('year'))

        if not (Month_Test and Day_Test and Year_Test):
            self.response.out.write(form)
        else:
            self.response.out.write("Thanks! That's a totally valid day!")


application = webapp2.WSGIApplication([
    ('/', MainPage),
], debug=True)

You can move all of the date related methods into a file called date_utils.py and import the date_utils in your current file as:

import date_utils

and in each of the method invocation, qualify it with the module name. For example:

Month_Test = valid_month(self.request.get('month'))

now becomes:

Month_Test = date_utils.valid_month(self.request.get('month'))

Note that, this way of importing methods in other file will work only as long as both the files are in the same directory. If your project structure is something like:

my_project
|__utils
|  |__file_utils.py
|__my_module
   |__main.py

and you want to include the methods in file_utils in main , you would have to make sure that my_project is in PYTHONPATH . Only then you can do an import in main like import utils.file_utils or from utils.file_utils import read_file .

Hope this helps.

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