简体   繁体   English

如何导入自己的python文件?

[英]How do I import my own python files?

In a file named BinarySearch.py I have the following: 在名为BinarySearch.py的文件中,我具有以下内容:

class SearchResult:    
    def __init__(self):
        self.was_found = False
        self.index = 0
    def __str__(self): 
        s = "SearchResult"
        s = s + "  was found:  "
        s = s + str(self.was_found) + "index:  " + str(self.index)
        return s

In another file, let's say it is named file2.py I have: 在另一个文件中,我们将其命名为file2.py

import os
cwd = os.getcwd()
import sys
sys.path.append(cwd)

import BinarySearch

However, when I try to run file2.py , I get the following error message: 但是,当我尝试运行file2.py ,出现以下错误消息:

NameError: name 'SearchResult' is not defined

It looks like the import BinarySearch did not actually import BinarySearch 看起来import BinarySearch实际上没有导入BinarySearch

I am using the Spyder IDE. 我正在使用Spyder IDE。 Both files ( BinarySearch.py and file2.py ) are in the same directory. 这两个文件( BinarySearch.pyfile2.py )都位于同一目录中。 Also, I went to Tool > PYTHONPATH manager and added the directory to the path. 另外,我转到工具> PYTHONPATH管理器,并将目录添加到路径。 I also tried restarting spyder to see if that was what was required for the path change to go into effect. 我还尝试重新启动间谍程序,以查看是否这是使路径更改生效所需的条件。 It still doesn't work. 它仍然不起作用。

EDIT: 编辑:

The line in file2.py which threw the error was the following: file2.py中引发错误的行如下:

sr = SearchResult()

Originally I assumed that the statement import BinarySearch would have the same behavior as if I copied the entire contents of BinarySearch.py and pasted it right where the import statement was inside file2.py. 最初,我假定语句import BinarySearch具有与复制BinarySearch.py​​的全部内容并将其粘贴到import语句在file2.py内的位置相同的行为。 I see now that's not how import works. 我现在看到import不是这样的。

The current directory is already in the path without you having to explicitly put it there in any way (through the IDE or through sys.path) 当前目录已经在路径中,而无需以任何方式(通过IDE或sys.path)将其明确放置在路径中

In your second piece of code you're missing the last line which I asume is giving you the error, and most likely is something like 在第二段代码中,您缺少最后一行,我认为这是给您的错误,很可能是

print(SearchResult())

It should be 它应该是

print(BinarySearch.SearchResult())

Or you could change your import to 或者您可以将导入更改为

from BinarySearch import SearchResult

And then you can just do 然后你可以做

print(SearchResult())

One solution seems to be to change the import statement from 一种解决方案似乎是将import语句从

import BinarySearch

to: 至:

from BinarySearch import *

The star/asterisk essentially means "import all," which imports everything from the file BinarySearch.py , including the SearchResult class. 星号/星号从本质上讲是“全部导入”,它从BinarySearch.py文件(包括SearchResult类)中导入所有内容。

The regular plain simple import also imports everything, but forces you to access things through a namespace. 常规的普通简单导入也会导入所有内容,但是会强制您通过名称空间访问内容。 Everything from the file BinarySearch.py is now inside the namespace BinarySearch . 文件BinarySearch.py内容现在都位于名称空间BinarySearch We could leave the original import statement alone, but anytime we use something in file2.py which comes from BinarySearch we would have to add a prefix. 我们可以file2.py会原始的import语句,但是file2.py我们在file2.py中使用来自BinarySearch ,就必须添加前缀。 We would have code that looks like this: 我们将拥有如下代码:

# inside file named file2.py
sr = BinarySearch.SearchResult()

If we get tired of writing BinarySearch. 如果我们厌倦了编写BinarySearch. before things all the time, we can create an alias for the namespace, like so: 一直以来,我们都可以为名称空间创建别名,如下所示:

import BinarySearch as bs

Then, inside file2.py , the statement sr = bs.SearchResult() will work just fine. 然后,在file2.py内部,语句sr = bs.SearchResult()将正常工作。

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

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