Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 06-16-2013, 02:51 PM   #1
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:

#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:

~$ Fighting Game/
There's no problem doing this is script1a:

import script1b
But in doing this:
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:

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
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,276

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

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

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

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

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.

init-hook=import site; site.addsitedir('.')
2 members found this post helpful.


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 > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:38 PM.

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