tpascale
tpascale

Reputation: 2576

imports & pathnames on windows

newbie Python question, 2.7.2 on Windows XP

I saved a one-line hello.py file [print "hello there"] in the current directory.

At the command prompt I use import to run the command

 >>> import hello
 hello there

ok - that's fine.

But what do you type if hello.py were in a subfolder of the current directory called "tempFolder"?

Upvotes: 1

Views: 68

Answers (2)

gecco
gecco

Reputation: 18870

The module is searched in Python's module search path.

You have two solutions:

  1. Make the folder tempFolder be a python package: Add a file named __init__.py in the folder tempFolder

  2. Add the folder tempFolder to the PYTHONPATH

Upvotes: 1

Lelouch Lamperouge
Lelouch Lamperouge

Reputation: 8421

Assuming this is your directory structure:

currentdir
    /tempFolder
        hello.py

Short Answer:

You should have a __init__.py in your directory of import. So it should look like:

currentdir
    /tempFolder
        __init__.py
        hello.py

Note: the __init__.py could be blank. There is no harm in that.

Check the official documentation for more information on this

Upvotes: 2

Related Questions