top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration
Print Preview

How to use relative Import in python

0 votes
215 views

I want to know how relative imports (in Python) are different than import. I have found lots of examples on internet explaining about the relative import but I want to know about the basic aspects of relative import that make it different than import.

posted May 16, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

1 Answer

0 votes

Relative imports search the directory containing the current package, absolute imports search the entire contents of sys.path. That is the only difference.

The purpose of relative imports is to make it easy for modules inside a package to import each other without worrying about other modules *outside* the package being imported by mistake. You can read the PEP here:

http://www.python.org/dev/peps/pep-0328/

to learn about the motivation for absolute and relative imports. Optional in Python 2.5 and 2.6, and compulsory in 2.7, when you write:

import moduleX

Python looks at sys.path for a list of directories to search for "moduleX.py". This is an absolute import. But consider a package with this structure:

package/
 +-- __init__.py
 +-- moduleA.py
 +-- moduleB.py
 +-- moduleX.py
 +-- subpackage/
 +-- __init__.py
 +-- moduleY.py
 +-- moduleZ.py

Inside moduleA, if you write "import moduleX" it will be an absolute import, the entire sys.path will be searched, and the "top level" module.X will be found.

But if you write this instead:

from . import moduleX

you have a relative import, and Python will only search the current package directory, so it will find the moduleX inside the package.

You can also write:

from .subpackage import moduleZ

to find the moduleZ inside the subpackage, again without searching the

entire sys.path.

Inside moduleY in the subpackage, all of these will find the same moduleZ:

# Relative to the current module:
from . import moduleZ

# Relative to the parent of the current module:
from ..subpackage import moduleZ
from .. import subpackage.moduleZ

# Absolute:
import package.subpackage.moduleZ
answer May 16, 2013 by anonymous
Similar Questions
+1 vote

I remember to avoiding to confusing compiler for header, use

// in my include file
#define MYHEADER_H
// and in others code use:
#ifndef MYHEADER_H
#include blahblah

Unfortunately, I prevent to same error, double import to python, but I don't know how to solve, Please help.

0 votes

Im required to import ha certain dll called 'NHunspell.dll' which is used for Spell Checking purposes. I am using Python for the software. Although I checked out several websites to properly use ctypes, I have been unable to load the dll properly.

When I use this code.

 from ctypes import *
 hunspell = cdll.LoadLibrary['Hunspellx64.dll']

I get an error

 hunspell = cdll.LoadLibrary['Hunspellx64.dll']
 TypeError: 'instancemethod' object has no attribute '__getitem__'

I guess it might a problem with the structure of the dll. But I have no idea how to import the dll properly.

+3 votes

I'm trying to import our team's old subversion repository to git, but I'd like to retain the commit history. I tried 'git svn clone' but that only retrieves commits from the last copy onwards.

Because the svn setup is really bad, there is no way I can reproduce the "stdlayout" structure that 'git svn' likes, or any other structure where the trunk isn't a just few versions down from a copy.

Is there a way to have 'git svn' not do "--stop-on-copy" when fetching history? I'm perfectly fine with getting a simple linear history (because trying to do anything else with our svn setup will put our sanity in danger), but I couldn't find any documentation on how to do so.

+4 votes

When I try to use numpy to deal with my dataset in the style of csv, I face a problem.

In my dataset of the csv file, some columns are string that can not convert to float easily. Some of them can ignore, but other columns I need to change the data to a enum style.

for example, one column just contain three kinds : S,Q,C. Each of them can declare one meaning, so I must convert them to a dict just like {1,2,3}

Now the question is, when I use numpy.loadtxt, I must do all things above in just one line and one function.


Useful Links with Similar Problem
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#470/147, 3rd Floor, 5th Main,
HSR Layout Sector 7,
Bangalore - 560102,
Karnataka INDIA.
QUERY HOME
...