Python 如何使用外部 .py 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3980059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:40:21  来源:igfitidea点击:

How do I use an external .py file?

python

提问by

I downloaded beautifulsoup.py for use on a little project I'm making. Do I need to import this .py file in my project?

我下载了 beautifulsoup.py 用于我正在制作的一个小项目。我需要在我的项目中导入这个 .py 文件吗?

Do I just copy and paste the code somewhere inside my current python script?

我是否只是将代码复制并粘贴到当前 python 脚本中的某处?

Thank you for the help.

感谢您的帮助。

I found this but it doesn't say anything regarding Windows. http://mail.python.org/pipermail/tutor/2002-April/013953.html

我发现了这个,但它没有说明任何关于 Windows 的内容。 http://mail.python.org/pipermail/tutor/2002-April/013953.html

I'm getting this error when using it. I copied and pasted the .py file to the folder where my project was on Windows Explorer, not this happens. Any suggestions? alt text

我在使用它时收到此错误。我将 .py 文件复制并粘贴到我的项目在 Windows 资源管理器上的文件夹中,但不会发生这种情况。有什么建议? 替代文字

回答by Sean Vieira

If it's in the same directory as your little project, all you should need to do is:

如果它与你的小项目在同一个目录中,你需要做的就是:

import BeautifulSoup

If you are keeping it in some other directory, the easiest way to do it is:

如果您将其保存在其他目录中,最简单的方法是:

from sys import path
path.append(path_to_Beautiful_Soup)

import BeautifulSoup

Python keeps track of where it is currently, and first looks in the current directory. Then it checks through all of the paths in sys.pathfor the module in question. If it cannot find it in any of those places, it throws an error.

Python 会跟踪它当前的位置,并首先在当前目录中查找。然后它会检查sys.path相关模块的所有路径。如果它在任何这些地方都找不到它,它就会抛出一个错误。

回答by Charlie Martin

You have several choices:

您有多种选择:

  • you can cut and paste in the code, assuming the license permits etc. however, what happens when the code is updated?

  • you can put the code into the same directory (ie folder) as your code. Then all you need to do is say import beautifulsoupbefore you try to use it.

  • you can put the code somewhere in the python load path.

  • 你可以剪切和粘贴代码,假设许可证等。但是,当代码更新时会发生什么?

  • 您可以将代码放入与您的代码相同的目录(即文件夹)中。然后你需要做的就是import beautifulsoup在尝试使用它之前说出来。

  • 您可以将代码放在 python 加载路径中的某个位置。

回答by ChristopheD

When you install beautifulsoup the canonical way (with easy_installfor example, or with a windows installer, if any) the beautifulsoup module will probably be added to your PYTHONDIR\lib\site-packagesdirectory.

当您以规范方式安装 beautifulsoup 时(easy_install例如,或使用 Windows 安装程序,如果有的话),beautifulsoup 模块可能会被添加到您的PYTHONDIR\lib\site-packages目录中。

This means

这意味着

import beautifulsoup

should do the trick.

应该做的伎俩。

Otherwise, adding beautifulsoup.py (if it's a single file) to your current project directory and then issuing import beautifulsoupshould also do the trick.

否则,将 beautifulsoup.py(如果它是单个文件)添加到您当前的项目目录然后发布import beautifulsoup也应该可以解决问题。

回答by Pwnna

I've done it before, putting BeautifulSoup.py inside the same directory as the script and import it would work. If you have multiple scripts spread over different directories, put the beautifulsoup file in the root directory and do a relative import.

我以前做过,将 BeautifulSoup.py 放在与脚本相同的目录中并导入它会起作用。如果您有多个脚本分布在不同的目录中,请将 beautifulsoup 文件放在根目录中并进行相对导入。

回答by mouad

you have to install a new package correctly in python by using in the command line:

你必须通过在命令行中使用在 python 中正确安装一个新包:

pip install BeautifulSoup 

if you don't know the name of the package use :

如果您不知道包的名称,请使用:

pip search beautiful 

and the pip will get all package that have "beautiful" in their name or description ...

并且 pip 将获得所有在其名称或描述中包含“漂亮”的包...

One more thing that is very important ; and because you use eclipse (netbeans it's the same) and pydev as i can see; you should refresh the list of package used by pydev when installing a new package by going in the menu to (for eclipse) Window -> Preference -> Pydev -> Interpreter - Pythonand clicking on Applywhy is that ?, so that you can use the full power of pydev correctly (code completion , F3 ...) and because Pydev don't know if a package has been added until you tell him so.

还有一件非常重要的事情;因为你使用 eclipse(netbeans 它是一样的)和 pydev,正如我所看到的;在安装新软件包时,您应该刷新 pydev 使用的软件包列表,方法是在菜单中转到(对于 eclipse)Window -> Preference -> Pydev -> Interpreter - Python并单击Applywhy is that?,以便您可以正确使用 pydev 的全部功能(代码完成,F3 ...),因为在您告诉他之前,Pydev 不知道是否添加了包。

the steps are for eclipse you can do their analog in netbeans right ?

这些步骤是针对 eclipse 的,您可以在 netbeans 中进行模拟,对吗?