Separating Scripts & Importing

This is the place for queries that don't fit in any of the other categories.

Separating Scripts & Importing

Postby JTeagle » Sat Apr 05, 2014 9:13 am

I'm very new to Python (and frankly, to Linux), so please be gentle.

I'm starting out trying to make use of some existing scripts on a Linux-based system (specifically, a Raspberry Pi, but I doubt that matters). Those scripts are all in their own little directory structure, but they are not part of the Linux system and so are not in the default search path. They each import various other files from within their system, so their relative paths must remain intact.

I don't want to lump my files in the same root directory, since I want to treat their scripts as a 3rd party library. However, I have seen mention that trying to use relative paths for importing is frowned upon. I had tried to use

Code: Select all
from ../../bla/bla/bla import xxx


but it refused, saying invalid syntax. I guess relative only means subdirectories. I had previously tried imp.load_source() as I had found elsewhere, but that then fell over because it couldn't resolve the imports within *that* file - because I'm guessing it treated it as the script now being in *my* directory.

For the moment while I'm just finding my feet I don't mind just putting my script in their directory, but does anyone have any suggestions as to how I can achieve better separation of code? Is adding their directory to the system path (however you do that) the only way?

For reference, this is roughly the structure as it stands:

Their scripts:

Code: Select all
/home/pi/subdir1/:
    file1.py
    file2.py (imports file1 and file3 from subdir2)
    subdir2/:
        file3.py


My (hopefully) scripts:

Code: Select all
/home/jason/subdir10/:
    file10.py
    file11.py (wants to import file2 from above)



Can their scripts be compiled into a library of some sort that is easier to import? Note that their file structure may include non-Python files that they manipulate.

Thanks in advance for any advice.
Last edited by stranac on Sat Apr 05, 2014 10:25 am, edited 1 time in total.
Reason: Added code tags. Locked.
JTeagle
 
Posts: 4
Joined: Sat Apr 05, 2014 8:16 am

Re: Separating Scripts & Importing

Postby metulburr » Sat Apr 05, 2014 11:31 am

Code: Select all
from ../../bla/bla/bla import xxx

That is not proper import syntax, the equivalent would be:
Code: Select all
from ..bla.bla.bla import xxx


you should read through some tutorials of modules
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Separating Scripts & Importing

Postby JTeagle » Tue Apr 08, 2014 3:04 pm

Thank you very much - that was a very useful article. I will use the .pth method.
JTeagle
 
Posts: 4
Joined: Sat Apr 05, 2014 8:16 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests