Ofek .T.
Ofek .T.

Reputation: 762

import modules from folder (Python)

I have a python file "a.py", a folder named folder and in the folder it has "b.py".

a.py has the code:

from folder.b import *
function()

It says:

NameError: name 'function' is not defined

It is defined.

Why? Thank you!

Upvotes: 2

Views: 128

Answers (2)

CT Zhu
CT Zhu

Reputation: 54340

Do you have to use folder.b? If not: You can add your folder name folder to system path:

import sys
sys.path.append(your_folder_containing_b.py)

And change a.py to:

from b import *

A less straight forward way is to change current working directory to folder and then from b import *

import os
os.chdir(your_folder_containing_b.py)

Upvotes: 1

Mario Rossi
Mario Rossi

Reputation: 7799

You probably need to define PYTHONPATH properly, making sure that it contains folder's parent folder.

Upvotes: 1

Related Questions