日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python: How to import other Python files

發布時間:2025/4/16 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python: How to import other Python files 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原文地址:http://stackoverflow.com/questions/2349991/python-how-to-import-other-python-files/20749411#20749411

There are many ways to import a python file, all with their pros and cons.

Don’t just hastily pick the first import strategy that works for you or else you’ll have to rewrite the codebase later on when you find it doesn’t meet your needs.

I’ll start out explaining the easiest example #1, then I’ll move toward the most professional and robust example #5

Example 1, Import a python module with python interpreter:

Put this in /home/el/foo/fox.py:
def what_does_the_fox_say():
print(“vixens cry”)
Get into the python interpreter:

el@apollo:/home/el/foo$ python
Python 2.7.3 (default, Sep 26 2013, 20:03:06)

import fox
fox.what_does_the_fox_say()
vixens cry

You imported fox through the python interpreter, invoked the python function what_does_the_fox_say() from within fox.py.
Example 2, Use execfile or (exec in Python 3) in a script to execute the other python file in place:

Put this in /home/el/foo2/mylib.py:
def moobar():
print(“hi”)
Put this in /home/el/foo2/main.py:
execfile(“/home/el/foo2/mylib.py”)
moobar()
run the file:

el@apollo:/home/el/foo$ python main.py
hi
The function moobar was imported from mylib.py and made available in main.py
Example 3, Use from … import … functionality:

Put this in /home/el/foo3/chekov.py:
def question():
print “where are the nuclear wessels?”
Put this in /home/el/foo3/main.py:
from chekov import question
question()
Run it like this:

el@apollo:/home/el/foo3$ python main.py
where are the nuclear wessels?
If you defined other functions in chekov.py, they would not be available unless you import *
Example 4, Import riaa.py if it’s in a different file location from where it is imported

Put this in /home/el/foo4/stuff/riaa.py:
def watchout():
print “my message”
Put this in /home/el/foo4/main.py:
import sys
import os
sys.path.append(os.path.abspath(“/home/el/foo4/stuff”))
from riaa import *

watchout()
Run it:

el@apollo:/home/el/foo4$ python main.py
my message
That imports everything in the foreign file from a different directory.
Example 5, Import files in python with the bare import command:

Make a new directory /home/el/foo5/
Make a new directory /home/el/foo5/herp
Make an empty file named init.py under herp:
el@apollo:/home/el/foo5/herp$ touch init.py
el@apollo:/home/el/foo5/herp$ ls
init.py
Make a new directory /home/el/foo5/herp/derp
Under derp, make another init.py file:
el@apollo:/home/el/foo5/herp/derp$ touch init.py
el@apollo:/home/el/foo5/herp/derp$ ls
init.py
Under /home/el/foo5/herp/derp make a new file called yolo.py Put this in there:
def skycake():
print “SkyCake evolves to stay just beyond the cognitive reach of ” +
“the bulk of men. SKYCAKE!!”
The moment of truth, Make the new file /home/el/foo5/main.py, put this in there;
from herp.derp.yolo import skycake
skycake()
Run it:

el@apollo:/home/el/foo5$ python main.py
SkyCake evolves to stay just beyond the cognitive reach of the bulk
of men. SKYCAKE!!
The empty init.py file communicates to the python interpreter that the developer intends this directory to be an importable package.
If you want to see my post on how to include ALL .py files under a directory see here: http://stackoverflow.com/a/20753073/445131

Example 6, use os.system(“python yourfile.py”)

import os
os.system(“python yourfile.py”)
Bonus protip

whether you are using Mac, Linux or Windows, you need to be using python’s idle editor as described here. It will unlock your python world. http://www.youtube.com/watch?v=DkW5CSZ_VII

總結

以上是生活随笔為你收集整理的Python: How to import other Python files的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。