Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python3. ImportError: no module named 'myfile'

Tags:

python

unix

macos

iMac-Mark:~ Mark$ python3
Python 3.3.2 (v3.3.2:d047928ae3f6, May 13 2013, 13:52:24) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import myfile
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'myfile'

I am new to Python and programming in general. I read a bit about sys.path, __PATH__ but I didn't understand anything.

like image 719
Mark Avatar asked Nov 20 '13 15:11

Mark


1 Answers

Solutions:

  1. Have you __init__.py file near the myfile.py?
  2. Do you have you directory in sys.path? (if not add it like this sys.path.append('path/to/python/code'))
  3. If you try import it from python enviroment you need run python shell near this file

Also you can try run this code

import sys
print(sys.path)

In your output you can see list of all directories which python see. If your file in any another directory you must change directory or python sys.path

You can do like this

$ python3
>>> import sys
>>> sys.path.append('/all/path/to/Desktop/Python')
>>> import myfile

Or like this

$ cd ~/Desktop/Python
$ python3
>>> import myfile
like image 66
itdxer Avatar answered Nov 11 '22 15:11

itdxer