Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python unable to import module

I have my program set up using packages as followed:

-base
    -init.py
    -base_class.py
-test
    -init.py
    -test.py

When I do the import statement from base.base_class import BaseClass in the test.py I get this error when running it:

from base.base_class import BaseClass
ImportError: No module named base.base_class

How can I import this module?

like image 855
Greg Brown Avatar asked Jul 31 '13 16:07

Greg Brown


People also ask

Why can't I import modules in Python?

This is caused by the fact that the version of Python you're running your script with is not configured to search for modules where you've installed them. This happens when you use the wrong installation of pip to install packages.

How can I import module in Python?

Import in python is similar to #include header_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is the most common way of invoking the import machinery, but it is not the only way.

How do I resolve the unable to import module error that I receive when I run Lambda code in Python?

To resolve this error, create a deployment package or Lambda layer that includes the libraries that you want to use in your Python code for Lambda. Important: Make sure that you put the library that you import for Python inside the /python folder.


1 Answers

at the top of test.py add

import sys
sys.path.append("..")

base is not a folder on the path...once you change this it should work

or put test.py in the same folder as base. or move base to somewhere that is on your path

like image 195
Joran Beasley Avatar answered Oct 10 '22 00:10

Joran Beasley