Nathik Azad
Nathik Azad

Reputation: 31

importing modules from packages in python, such a basic issue I guess I can't even find the answer anyhwere

So my first python program, I downloaded the macholib package from PYPI, then unzipped it and ran installed the setup.py using python install, now I'm trying to run a program where I first import the macholib, then when I try to access methods like Macho, it gives me an error saying macholib module has no attributes called Macho. My understanding is macholib is a package and not a module or something, hence I cant use the contents of the package. Please answer, I've wasted too much time on such a simple newbie issue. running a mac with the latest version of python.

Code:

import sys
import macholib
MachO(DivXInstaller.dmg) 

I tried macholib.MachO(DivXInstaller.dmg) and macholib.MachO.MachO(DivXInstaller.dmg)

Error for python test.py 
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    MachO(DivXInstaller.dmg)
NameError: name 'MachO' is not defined

Upvotes: 2

Views: 947

Answers (3)

jurgenreza
jurgenreza

Reputation: 6086

This is so simple if you understand Python Packages vs. Modules and import vs. from import:

A Python module is simply a Python source file.

A Python package is simply a directory of Python module(s).

1- import:

import package1.package2.module1

To access module1 classes, functions or variables you should use the whole namespace: package1.package2.modlue1.class1

You cannot import a class or function this way: (wrong)

import package1.package2.class1 

2- from ... import

Instead use "from" to import a single class, function or variable of a module:

from package1.package2.module1 import class1

no need to address the whole namespace: class1.method1() works now

Note that you cannot import a method of a class this way

Example:

datetime is a class of module datetime that has a method called utcnow(), to access utcnow() one could:

import datetime
datetime.datetime.utcnow()

Or

from datetime import datetime
datetime.utcnow()

Upvotes: 0

Andrew Jaffe
Andrew Jaffe

Reputation: 27097

An import statement defines a namespace. Hence, in order to use something defined in the module or package, you need to qualify it. In this case, the module itself is macholib.MachO but you need to dig deeper, to the actual class MachO which, slightly confusingly, has the same name, so you need to import macholib.MachO and use macholib.MachO.MachO (note that when I try to do this I get an error "DistributionNotFound: altgraph", however).

Note further that it takes a filename as an argument, which is a string. Moreover, you are actually creating an instance of a class, so you probably mean

mlib = macholib.MachO.MachO("DivXInstaller.dmg") 

where now mlib is actually the object you need to manipulate with further calls...

Alternately you can do from macholib import MachO or even from macholib.MachO import MachO so you could use MachO.MachO(...) or MachO(...) directly.

Upvotes: 2

Lennart Regebro
Lennart Regebro

Reputation: 172319

You have this line in your code:

MachO(DivXInstaller.dmg) 

However, the name MachO is not defined anywhere. So Python tells you this with the error

NameError: name 'MachO' is not defined

If you want to use the MachO module under the name MachO, you have to import it:

from macholib import MachO

This you have not done, which is the cause of your error. All you did was

import macholib

which gives you the name "macholib" that you can use. However, macholib contains mostly submodules which you can not access like that, so that is not particularly useful. If you don't want to pollute the namespace, you can import MachO as

import machlibo.MachO

Which gives you access to the MachO module as macholib.MachO

You haven't defined DivXInstaller.dmg either, so that's going to be your next error. I recommend that you go through a Python tutorial before you start programming in it.

Upvotes: 5

Related Questions