简体   繁体   中英

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". 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.

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.

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:

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:

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/

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.

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

It is not the same class as instantiating an object from within 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. ie

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

The easiest way to use python code in other files is to use import statement.

Say when you do

import xyz 

Python will attempt to find the file 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)
  2. Locations mentioned in PYTHONPATH environment variable (or sys.path in python)
  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

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

python line_segment.py

, it should work (You can optionally also make a blank file init .py in the same folder).

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. To be able to run it from everywhere, before you run it add location of line.py to the 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. For Windows unfortunately I do not know the procedure of modifying PYTHONPATH

Since they're in the same directory, create an empty file named __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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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