Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting the following error in Python "ImportError: No module named py"?

Tags:

I'm a Python newbie, so bear with me :)

I created a file called test.py with the contents as follows:

test.py import sys print sys.platform print 2 ** 100 

I then ran import test.py file in the interpreter to follow an example in my book. When I do this, I get the output with the import error on the end.

win32 1267650600228229401496703205376 Traceback (most recent call last):   File "<stdin>", line 1, in <module> ImportError: No module named py 

Why do I get this error and how do I fix it? Thanks!

like image 210
jtbradle Avatar asked Jan 28 '09 21:01

jtbradle


People also ask

How do I fix the ImportError No module named error in Python?

To get rid of this error “ImportError: No module named”, you just need to create __init__.py in the appropriate directory and everything will work fine.

How do I fix an import module error in Python?

Python's ImportError ( ModuleNotFoundError ) indicates that you tried to import a module that Python doesn't find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH .

What is __ init __ py for?

The __init__.py file makes Python treat directories containing it as modules. Furthermore, this is the first file to be loaded in a module, so you can use it to execute code that you want to run each time a module is loaded, or specify the submodules to be exported.

Can not import name Python?

The Python "ImportError: cannot import name" occurs when we have circular imports (importing members between the same files). To solve the error, move the objects to a third file and import them from a central location in other files, or nest one of the imports in a function.


1 Answers

Instead of:

import test.py 

simply write:

import test 

This assumes test.py is in the same directory as the file that imports it.

like image 86
DzinX Avatar answered Oct 20 '22 04:10

DzinX