top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to use relative Import in python

0 votes
759 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 LinkedIn 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.

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

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.

0 votes

The last few days I've been working on a script to manipulate some scientific data. One thing I would like to be able to do is find relative maxima in a data set.
I'm using numpy in python3 (which I think I can't do without because of utf16 encoding of my data source) and a series of np.arrays. When looking around the web and some forums I came across the scipy function argrelextrema, which seemed to do just what I wanted. The problem is that I can't get the function to work, probably because scipy in python3 does not yet support the argrelextrema function. I can however, not find a reference to this really being the problem, and was wondering if someone here could maybe help me out.
The code I used is shown below. The function to return the maximum values is called by a different script. Then, when running, it returns an error like the one below the code.

Script:
import numpy as np
import scipy as sp

def max_in_array_range(MaxArray, Column, LowBound):

 "Finding the max value an array with a predefined in a certain column and from a threshold index.
 The complete array is loaded through Max Array. The column is first selected. 
 The, the LowBound is called (the data is only interesting from a certain point onward."
 TempArray = MaxArray[:, Column] # Creation of Temporary Array to ensure 1D Array and specification of the LowBound in the next line.
 return sp.argrelextrema(TempArray[LowBound:], np.greater)

Error message:
Traceback (most recent call last):
 File "MyScript.py", line 15, in 
 Varrr = FD.max_in_array_range(CalcAndDiffArray, 5 ,MyBound)
 File "/MyPath/Script.py", line 82, in max_in_array_range
 return sp.argrelmax(TempArray[LowBound:], np.greater)
AttributeError: 'module' object has no attribute 'argrelmax'
...