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

时间:2021-03-29 23:56:27

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? 我如何使用外部.py文件?

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

5 个解决方案

#1


9  

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.path for the module in question. If it cannot find it in any of those places, it throws an error.

Python会跟踪当前的位置,并首先查看当前目录。然后,它检查sys.path中所有相关模块的路径。如果它无法在任何这些地方找到它,则会引发错误。

#2


5  

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

当您以规范的方式安装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 beautifulsoup should also do the trick.

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

#3


1  

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 beautifulsoup before you try to use it.

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

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

    你可以把代码放在python加载路径的某个地方。

#4


0  

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文件放在根目录中并进行相对导入。

#5


0  

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 - Python and clicking on Apply why 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并单击Apply为什么会这样?,这样你就可以了正确使用pydev的全部功能(代码完成,F3 ...),因为Pydev不知道是否添加了包,直到你告诉他。

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

eclipse的步骤你可以在netbeans中做他们的模拟吗?

#1


9  

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.path for the module in question. If it cannot find it in any of those places, it throws an error.

Python会跟踪当前的位置,并首先查看当前目录。然后,它检查sys.path中所有相关模块的路径。如果它无法在任何这些地方找到它,则会引发错误。

#2


5  

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

当您以规范的方式安装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 beautifulsoup should also do the trick.

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

#3


1  

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 beautifulsoup before you try to use it.

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

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

    你可以把代码放在python加载路径的某个地方。

#4


0  

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文件放在根目录中并进行相对导入。

#5


0  

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 - Python and clicking on Apply why 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并单击Apply为什么会这样?,这样你就可以了正确使用pydev的全部功能(代码完成,F3 ...),因为Pydev不知道是否添加了包,直到你告诉他。

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

eclipse的步骤你可以在netbeans中做他们的模拟吗?