简体   繁体   English

如何从文件导入python代码?

[英]How do I import python code from a file?

I was hesitant about asking this because I've seen similar questions with overly complicated answers. 我对这个问题犹豫不决,因为我看到类似的问题,答案过于复杂。 The question I actually want answered to was closed on account of being "vague" and "not a real question", so I will be as specific as possible. 我真正想要回答的问题因“模糊”和“不是真正的问题”而被关闭,所以我将尽可能具体。

I have an object called "line" in a file called "line.py". 我在名为“line.py”的文件中有一个名为“line”的对象。 I want to create another object that inherits from "line" called "line_segment" and put it in a file called "line_segment.py" in the same directory. 我想创建另一个继承自“line”的对象,称为“line_segment”,并将其放在同一目录中名为“line_segment.py”的文件中。

path_finding_lib/
   line.py
   line_segment.py
   a_star.py

The problem is i can't seem to find a way to use the code in "line.py" from inside "line_segment.py" without appending strings to the system PATH variable and stuff like that. 问题是我似乎找不到从“line_segment.py”内部使用“line.py”中的代码的方法,而不将字符串附加到系统PATH变量和类似的东西。

Isn't there a way to import from a file path of something like that? 有没有办法从这样的文件路径导入? If not, why not? 如果没有,为什么不呢?

While you could append to the python path for a particular file using: 虽然您可以使用以下命令附加到特定文件的python路径:

import sys
sys.path.append('pathtoModule')

If they are in the same folder, (not a module, as you lack an init .py), you can simply import from the line module by doing: 如果它们位于同一文件夹中(不是模块,因为缺少init .py),您可以通过执行以下操作从行模块导入:

from line import Line
class LineSegment(line):

Naming convention supplies underscores and lowercases for modules, Capitalized for Classes: http://www.python.org/dev/peps/pep-0008/ 命名约定为模块提供下划线和小写,适用于类: http//www.python.org/dev/peps/pep-0008/

It is nonstandard and will likely lead to trouble if you dynamically append to the python path in your project, as it will cause errors in object comparison. 它是非标准的,如果你动态附加到项目中的python路径,可能会导致麻烦,因为它会导致对象比较中的错误。

If you have an object declared as follows: 如果您有一个声明如下的对象:

path_finding_lib/
   line.py
   line_segment.py
   a_star.py
   somemodule/
       afile.py

If your line module imports and instantiates an object from afile using the path somemodule.afile.SomeObject 如果您的行模块使用路径somemodule.afile.SomeObject从afile导入并实例化对象

It is not the same class as instantiating an object from within afile: 它与在afile中实例化对象的类不同:

afile.SomeObject.

If afile returns an instance of afile.SomeObject and the object is compared for equality to an instance of somemodule.afile.SomeObject, they will be found to be not equivalent. 如果afile返回一个afile.SomeObject的实例,并且将该对象与somemodule.afile.SomeObject的实例进行相等性比较,则会发现它们不相等。 ie

somemodule.afile.SomeObject == afile.SomeObject
==> False

The easiest way to use python code in other files is to use import statement. 在其他文件中使用python代码的最简单方法是使用import语句。

Say when you do 说你什么时候做

import xyz 

Python will attempt to find the file xyz.py. Python将尝试查找文件xyz.py. It looks into 它调查

  1. The site-packages folder (which is the folder in your python installation directory which contains pre-installed modules like say django etc) site-packages文件夹(这是python安装目录中的文件夹,其中包含预装的模块,比如说django等)
  2. Locations mentioned in PYTHONPATH environment variable (or sys.path in python) PYTHONPATH环境变量中提到的位置(或python中的sys.path)
  3. Your current directory 您当前的目录

In your case, your program should have the following line 在您的情况下,您的程序应该具有以下行

from line import line

Where first line is your line.py file and second line is your class Wherever you want to use line object for inheritance just use 第一行是你的line.py文件,第二行是你的类无论你想使用行对象进行继承,只需使用

class newline(line):

The catch is how you run the program. 问题是如何运行程序。 If you run it from within path_finding_lib (ie when your working directory is path_finding_lib and you do 如果你从path_finding_lib中运行它(即当你的工作目录是path_finding_lib而你做的时候

python line_segment.py

, it should work (You can optionally also make a blank file init .py in the same folder). ,它应该工作(您也可以选择在同一文件夹中创建一个空文件init .py)。

If you run it from say your home directory 如果你从你的主目录运行它

~$ python /path_to/path_finding_lib line_segment.py

It will NOT work. 不起作用。 This is because python will search site-packages, PYTHONPATH and your current directory and not find line.py. 这是因为python将搜索site-packages,PYTHONPATH和你当前的目录而不是line.py. To be able to run it from everywhere, before you run it add location of line.py to the PYTHONPATH 为了能够从任何地方运行它,在运行之前将line.py的位置添加到PYTHONPATH

$export PYTHONPATH=/path_to/path_finding_lib

Then you should be able to run it 然后你应该能够运行它

NOTE: I have assumed you have a linux ystem. 注意:我假设你有一个linux系统。 For Windows unfortunately I do not know the procedure of modifying PYTHONPATH 对于Windows,我不知道修改PYTHONPATH的过程

Since they're in the same directory, create an empty file named __init__.py . 由于它们位于同一目录中,因此请创建一个名为__init__.py的空文件。 This lets Python treat the directory you're working from as a package, and you'll be able to pull objects and methods from these other files. 这允许Python将您正在使用的目录作为包处理,并且您将能够从这些其他文件中提取对象和方法。

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

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