user3134370
user3134370

Reputation:

Calling modules in python

So Im a beginner to python/programming and came upon this code in a tutorial, which Im having trouble understanding.

from pythonds.basic.stack import Stack

What I did was , I went to the site-packages folder in my python directory (which holds all modules). There I could find the directory structure to be : -

 pythonds/basic/stack.py

The file stack.py has a "class Stack" inside it. So am I correct in interpreting/relating the import command to this directory structure ? Also , whenever such a long chaining of modules happen in python, can it always be understood in such a manner.

Upvotes: 0

Views: 1213

Answers (3)

Lily_Xu
Lily_Xu

Reputation: 11

In command line, you can do like this:

C:\Python27\Lib>pip intall pythonds

Then this module can work.

Upvotes: 1

Hema
Hema

Reputation: 167

Your understanding is right.

import pythonds.basic.stack

This will make all the classes in the module accessible by your script. Whereas,

from pythonds.basic.stack import Stack

will make only the Stack class accessible by your script.

Upvotes: 0

user707650
user707650

Reputation:

Not all the time.

It's probably better to not try and compare the directory structure with the module path, unless you have to debug modules or install them manually.

Sometimes, your PYTHONPATH will be extended to include subdirectories in site-packages, and then there'll be an extra subdirectory.

Other times, there can be an __init__.py file in the pythonds/basic/ directory (there likely is), that can contain

from .stack import Stack

in which case the import path could be

from pythonds.basic import Stack

Upvotes: 0

Related Questions