简体   繁体   English

从Python中的另一个文件导入不同的功能?

[英]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. 我有一个通过Google App Engine托管的python主文件,但是,我的所有函数都定义在同一类中。 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: 你可以将所有的日期相关的方法到一个文件名为date_utils.py并导入date_utils在当前文件为:

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 . 并且要在main file_utils中包含方法, file_utils必须确保my_projectPYTHONPATH Only then you can do an import in main like import utils.file_utils or from utils.file_utils import read_file . 只有这样,您才能在main进行导入,例如import utils.file_utilsfrom utils.file_utils import read_file

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM