您的位置:首页 > 编程语言 > Python开发

python调用其他文件的类和函数

2018-03-31 11:36 2541 查看
在同一个文件夹下

调用函数

source.py文件:

def func():
pass

new.py文件:

import source
# 或者
from  source import func

调用类

Student.py文件:

class Student:
def __init__(self, name, age, sex):
self name = name
self age = age
self sex =sex

def learn(self):
print("学生学习!")

handler.py文件:

from Student import Student
s = Student('egon', 18, 'male')
s.learn()

# 或者
import Student
s = Student.Student('jack', 28, 'male')
s.learn()
在不同一个文件夹下

由于Python import模块时,是在sys.path里按顺序查找的。需要先将要使用文件的文件路径加入sys.path中。

import sys
sys.path.appent(r'/home/admin/PythonProject/CourseSelesct/')
import Student

s = Student.Student('egon', 18, 'male')
s.learn()

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: