美文网首页
pyhton引用包问题

pyhton引用包问题

作者: 慧琴如翌 | 来源:发表于2018-03-06 18:33 被阅读6次

1.test1.py和test2.py均在同一目录下

test1.py想引用test2.py下的函数
在test1.py中写上以下即可调用test2中的函数

import test2
image.png

test1.py代码:

#coding=utf-8
import test2
def  test1_function(n):
    print 'in function test1_function:',n

if __name__=='__main__':
    test2.test2_function(3, 4)

test2.py代码:

#coding=utf-8
def  test2_function(m,n):
    print 'in function test2_function:',m+n

其中test1.py代码还可以是:

#coding=utf-8
from test2 import test2_function
def  test1_function(n):
    print 'in function test1_function:',n
if __name__=='__main__':
    test2_function(3, 4)

2. test1和test2的父目录下同一目录下

1)test1.py中引用test2.py中的函数

image.png

i)方式一:

test1.py中代码

#coding=utf-8
import dir1.test2 as test2

def  test1_function(n):
    print 'in function test1_function:',n

if __name__=='__main__':
    test2.test2_function(3, 4)

ii)方式二:
test1.py中代码:

#coding=utf-8

from dir1.test2 import test2_function
def  test1_function(n):
    print 'in function test1_function:',n

if __name__=='__main__':
    test2_function(3, 4)

2)test2.py想引用test1.py中的函数

i)方式一:

test2.py中代码:
#coding=utf-8
import test1

def  test2_function(m,n):
    print 'in function test2_function:',m+n

if __name__=='__main__':
    test1.test1_function(5)

ii)方式二:
test2.py中代码:

#coding=utf-8
from test1 import test1_function

def  test2_function(m,n):
    print 'in function test2_function:',m+n

if __name__=='__main__':
    test1_function(5)

3. 多层级之间模块的引用,如下例子test3.py中想引用test2.py中的函数

image.png

test3.py中代码:

#coding=utf-8

import sys
import os
reload(sys)
sys.setdefaultencoding('utf-8')
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
# print 'base_dir'
print 'basedir:',BASE_DIR

import ff.para.dir2.test2 as test2

# import dir2.test2 as test2


def  test3_function(s):
    print 'in function test3_function:',s

if __name__=='__main__':
    test2.test2_function(2,6)
    pass

注意:import ff.para.dir2.test2 as test2

4. 如果层级特别深,不想引用那么多的层级的话,可以使用sys.path.append(BASE_DIR)

image.png

test3.py中代码:

#coding=utf-8

import sys
import os
reload(sys)
sys.setdefaultencoding('utf-8')
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
# print 'base_dir'
print 'basedir:',BASE_DIR

import dir2.test2 as test2

def  test3_function(s):
    print 'in function test3_function:',s

if __name__=='__main__':
    test2.test2_function(2,6)
    pass

5. 上面的<meta content="text/html; charset=utf-8" http-equiv="Content-Type">sys.path.append()也可以用pycharm的mark directory as sources root功能取而代之,但是还是上面<meta content="text/html; charset=utf-8" http-equiv="Content-Type">sys.path.append()更通用一些

image.png

如果想使用pycharm的mark directory as sources root功能,将para右键上做该操作,依然使用上面的test3.py的代码还是可以执行,也可以改写test3.py的代码:

#coding=utf-8

import dir2.test2 as test2


def  test3_function(s):
   print 'in function test3_function:',s

if __name__=='__main__':
   test2.test2_function(2,6)
   pass

注意:import dir2.test2 as test2

相关文章

网友评论

      本文标题:pyhton引用包问题

      本文链接:https://www.haomeiwen.com/subject/rfprfftx.html