Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Python脚本导入另一个脚本?_Python_Python 2.7_Import - Fatal编程技术网

将Python脚本导入另一个脚本?

将Python脚本导入另一个脚本?,python,python-2.7,import,Python,Python 2.7,Import,我正在学习Zed Shaw的《艰苦学习Python》,现在开始学习第26课。在本课中,我们必须修复一些代码,这些代码从另一个脚本调用函数。他说我们不需要进口它们来通过测试,但我很好奇我们将如何做到这一点 | 下面是调用上一个脚本的特定代码行: words = ex25.break_words(sentence) sorted_words = ex25.sort_words(words) print_first_word(words) print_last_word(words) print_f

我正在学习Zed Shaw的《艰苦学习Python》,现在开始学习第26课。在本课中,我们必须修复一些代码,这些代码从另一个脚本调用函数。他说我们不需要进口它们来通过测试,但我很好奇我们将如何做到这一点

|

下面是调用上一个脚本的特定代码行:

words = ex25.break_words(sentence)
sorted_words = ex25.sort_words(words)

print_first_word(words)
print_last_word(words)
print_first_word(sorted_words)
print_last_word(sorted_words)
sorted_words = ex25.sort_sentence(sentence)
print sorted_words
print_first_and_last(sentence)
print_first_a_last_sorted(sentence)
要更正的代码:
  • 这是课程中的代码,正在被引用
    • 不要编辑问题以更正代码
def break_单词(东西):
“”“此函数将为我们分解单词。”“”
单词=东西。拆分(“”)
回话
def sort_单词(单词):
“对单词进行排序。”
返回已排序(字)
def打印第一个单词(单词)
“”“弹出后打印第一个单词。”“”
word=word.poop(0)
印刷字
def打印最后一个字(字):
“”“弹出后打印最后一个单词。”“”
word=words.pop(-1)
印刷字
def sort_句子(句子):
“”“接收完整的句子并返回已排序的单词。”“”
单词=打断单词(句子)
返回排序单词(单词)
def print_first_和_last(句子):
“”“打印句子的第一个和最后一个单词。”“”
单词=打断单词(句子)
打印第一个单词(单词)
打印最后一个单词(单词)
def print_first_和_last_排序(句子):
“”“对单词进行排序,然后打印第一个和最后一个单词。”“”
单词=排序句子(句子)
打印第一个单词(单词)
打印最后一个单词(单词)
打印“让我们练习一切。”
打印“您需要知道有关使用\\换行符和\t制表符进行转义的信息。”
诗篇=”“
\可爱的世界
有着如此坚定的逻辑
无法辨别\n爱的需要
也不能从直觉中理解激情
需要解释一下
\n\t\t这里没有。
"""
打印“------------”
印刷诗
打印“------------”
五=10-2+3-5
打印“这应该是五个:%s”%5
def秘密_公式(已启动):
果冻豆=已开始*500
罐子=果冻豆\1000
板条箱=罐/100
退回果冻豆、罐子、板条箱
起点=10000
豆子、罐子、板条箱==秘方(起点)
打印“起始点为:%d”%start\u point
打印“我们有%d条牛仔裤、%d个罐子和%d个板条箱。”%(豆子、罐子、板条箱)
起点=起点/10
打印“我们也可以这样做:”
打印“我们将有%d个豆子、%d个罐子和%d个海棠。”%secret\u公式(开始)
句子=“所有的上帝\t都属于有重量的人。”
单词=ex25。断开单词(句子)
排序单词=ex25。排序单词(单词)
打印第一个单词(单词)
打印最后一个单词(单词)
.打印第一个单词(已排序的单词)
打印最后一个单词(已排序的单词)
排序的单词=ex25。排序的句子(句子)
主要分类词
打印第一个和最后一个(句子)
打印第一个排序的最后一个排序的(senence)

这取决于第一个文件中的代码的结构

如果只是一堆函数,比如:

# first.py
def foo(): print("foo")
def bar(): print("bar")
然后您可以导入它并使用以下功能:

# second.py
import first

first.foo()    # prints "foo"
first.bar()    # prints "bar"

或者,要导入first.py中定义的所有名称:

# second.py
from first import *

foo()          # prints "foo"
bar()          # prints "bar"
注意:这假设两个文件位于同一目录中。


当您想从其他目录或包中的模块导入名称(函数、类等)时,它会变得更加复杂。

值得一提的是(至少在python 3中),为了使其正常工作,您必须在同一目录中有一个名为
\uuuu init\uuuu.py
的文件。

以下内容对我很有用,而且看起来也很简单:

假设我们想要导入一个脚本./data/get_my_file.py并想要访问其中的get_set1()函数

import sys
sys.path.insert(0, './data/')
import get_my_file as db

print (db.get_set1())
希望这能奏效

def break_words(stuff):
    """This function will break up words for us."""
    words = stuff.split(' ')
    return words

def sort_words(words):
    """Sorts the words."""
    return sorted(words)

def print_first_word(words):
    """Prints the first word after popping it off."""
    word = words.pop(0)
    print (word)

def print_last_word(words):
    """Prints the last word after popping it off."""
    word = words.pop(-1)
    print(word)

def sort_sentence(sentence):
    """Takes in a full sentence and returns the sorted words."""
    words = break_words(sentence)
    return sort_words(words)

def print_first_and_last(sentence):
    """Prints the first and last words of the sentence."""
    words = break_words(sentence)
    print_first_word(words)
    print_last_word(words)

def print_first_and_last_sorted(sentence):
    """Sorts the words then prints the first and last one."""
    words = sort_sentence(sentence)
    print_first_word(words)
    print_last_word(words)


print ("Let's practice everything.")
print ('You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.')

poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explantion
\n\t\twhere there is none.
"""


print ("--------------")
print (poem)
print ("--------------")

five = 10 - 2 + 3 - 5
print ("This should be five: %s" % five)

def secret_formula(start_point):
    jelly_beans = start_point * 500
    jars = jelly_beans / 1000
    crates = jars / 100
    return jelly_beans, jars, crates


start_point = 10000
jelly_beans, jars, crates = secret_formula(start_point)

print ("With a starting point of: %d" % start_point)
print ("We'd have %d jeans, %d jars, and %d crates." % (jelly_beans, jars, crates))

start_point = start_point / 10

print ("We can also do that this way:")
print ("We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_point))


sentence = "All god\tthings come to those who weight."

words =  break_words(sentence)
sorted_words =  sort_words(words)

print_first_word(words)
print_last_word(words)
print_first_word(sorted_words)
print_last_word(sorted_words)
sorted_words =  sort_sentence(sentence)
print (sorted_words)

print_first_and_last(sentence)
print_first_and_last_sorted(sentence)

我强烈推荐阅读SciPy讲座组织的讲座:

它解释了所有的疑问

但是,可以使用以下代码轻松添加新路径并避免重复:

import sys
new_path = 'insert here the new path'

if new_path not in sys.path:
    sys.path.append(new_path)
import funcoes_python #Useful python functions saved in a different script

啊,所以它们必须在同一个目录中…太棒了!谢谢!这对Python 3有什么变化?哪些符号不是通过第一个方法导入的?@Goldname在第一个方法中是唯一导入的符号(导入到导入器的全局命名空间中)第一个是
first
。第二个是
foo
bar
。第三个是所有符号(有一些注意事项,但特别包括first.py包含的任何导入)。可能有用:如果使用“import first.py”(而不是“import first”)则不起作用@LoveMeow是的,没问题。事实上我不是这样。如果没有你的脚本(你正在运行的脚本),它就可以正常工作并且您要从中导入的文件位于同一目录中,您不需要
\uuuu init\uuuuuu.py
文件。如果您的脚本位于包含您要导入的文件的目录之外,则您需要这样一个文件。更新到以前的注释,/Python3.3引入了“隐式命名空间包”,在许多情况下不需要空的
\uuuu init\uuuu.py
文件。我不知道为什么,但这是使它工作的唯一方法,至少对我来说是这样。
import sys
new_path = 'insert here the new path'

if new_path not in sys.path:
    sys.path.append(new_path)
import funcoes_python #Useful python functions saved in a different script