LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-16-2013, 02:51 PM   #1
Eahil
LQ Newbie
 
Registered: Feb 2013
Location: Rochester, New York
Distribution: Archlinux
Posts: 13

Rep: Reputation: Disabled
Lightbulb Python -- Importing Modules


Hi! I'm just wondering a bit here. With importing modules in Python, I usually go about it like this when I'm using standard modules:

Code:
#Standard module
import os
#No problems at runtime
But when I'm creating my own application, the directory structure might look a bit like this:

Code:
~$ Fighting Game/
    __init__.py
    dir1/
        script1a.py
        script1b.py
        __init__.py
    dir2/
        script2a.py
        script2b.py
        __init__.py
There's no problem doing this is script1a:

Code:
import script1b
But in doing this:
Code:
import script2a
An ImportError occurs and it claims it does not exist. I've attempted relative importing, but it doesn't seem to work even with:

Code:
import ..script2b
Is there a better way than adding the package in development, to my PYTHONPATH? I'd like to simply refer to the different modules in the different directories rather than go forth with just putting everything into the system itself.

Any help with this?

Last edited by Eahil; 06-16-2013 at 02:56 PM.
 
Old 06-16-2013, 11:14 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,731

Rep: Reputation: 488Reputation: 488Reputation: 488Reputation: 488Reputation: 488
If import accepts pathnames, then use proper pathnames:

Code:
bad: import ..script2b
good:import ../dir2/script2b
 
1 members found this post helpful.
Old 06-17-2013, 10:42 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Code:
script2a = __import__('dir2.script2a',level=1)
Kevin Barry
 
Old 06-17-2013, 02:30 PM   #4
jpkotta
LQ Newbie
 
Registered: Feb 2012
Posts: 3

Rep: Reputation: Disabled
The way I usually do this is with a .pth file and the site module.

dir1/deps.pth
Code:
../dir2
dir1/script1a.py
Code:
import site
site.addsitedir('.') # picks up the paths in dir1/deps.pth
You can use relative or absolute paths in both the .pth file and addsitedir() call. The path information can live in version control and you don't have to mess with environment variables. The way I usually arrange things is to have a deps.pth in the top level and the addsitedir() in any scripts that are expected to be __main__. addsitedir() does little more than adding paths to sys.path, so if the top level script does it, all the imported modules will see the paths too.

As a bonus, you can make this work with pylint so that it finds all of your local dependencies.

.pylintrc
Code:
[MASTER]
init-hook=import site; site.addsitedir('.')
 
2 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Yum corrupted on CentOS 6.2, Problem importing one the python modules. WassimZ Linux - Newbie 13 01-14-2013 01:36 PM
[SOLVED] [Python] Importing and using tkinter Mr. Alex Programming 3 09-22-2012 08:43 AM
Importing python script nishitmukesh Linux - Newbie 4 04-11-2011 01:56 AM
Ubuntu 9.10 - Error importing python modules - $PYTHONPATH? km0r3 Linux - Software 4 12-06-2009 09:18 PM
importing trouble....python...regarding Tkinter maseby Programming 0 11-03-2003 01:54 PM


All times are GMT -5. The time now is 07:15 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration