7

I am getting this error

Traceback (most recent call last):
File "Exporter.py", line 3, in <module>
import sys,getopt,got,datetime,codecs
File "C:\Users\Rohil\Desktop\GetOldTweets-python-master\got\__init__.py", line 1, in <module>
import models
ModuleNotFoundError: No module named 'models'

my directory tree is:

C:\Users\Rohil\Desktop\GetOldTweets-python-master\got

this contains 2 folders: manager and models and 1 __init__.py file with the code :

import models
import manager

i am executing a file with the path: C:\Users\Rohil\Desktop\GetOldTweets-python-master\Exporter.py

I can't figure out what the issue is. can anyone assist me?

Rohil
  • 177
  • 1
  • 3
  • 13

5 Answers5

5

Set the environment variable PYTHONPATH=C:\Users\Rohil\Desktop\GetOldTweets-python-master\got (how exactly, depends on your operating system)

blue_note
  • 27,712
  • 9
  • 72
  • 90
  • can you import the other module? also, make sure that your terminal sees the correct PYTHONPATH value – blue_note Jan 28 '17 at 13:30
  • created a new variable in windows called 'PythonPath' and added `C:\Users\Rohil\Desktop\GetOldTweets-python-master\got` as the value. Still throws the same error – Rohil Jan 28 '17 at 13:32
  • nope, cant import any module from that folder. How do i make sure that the terminal sees the correct pythonpath value on windows? – Rohil Jan 28 '17 at 13:33
  • the variable should be IN CAPITAL letters. then, at command prompt, try `echo %PYTHONPATH%` and check that you see the correct value – blue_note Jan 28 '17 at 13:34
  • okay now, python sees the got folder, but refuses to see the files inside the models folder. Throws this error now : `Traceback (most recent call last): File "Exporter.py", line 3, in import sys,getopt,got,datetime,codecs File "C:\Users\Rohil\Desktop\GetOldTweets-python-master\got\__init__.py", line 1, in import models File "C:\Users\Rohil\Desktop\GetOldTweets-python-master\got\models\__init__.py", line 1, in from Tweet import Tweet ModuleNotFoundError: No module named 'Tweet'` – Rohil Jan 28 '17 at 13:35
  • 1
    Well, it worked so far, now you have a different problem than before, need more info. Also, make use that your __init__.py is empty file (does not import anything) – blue_note Jan 28 '17 at 13:39
5

If you have created a directory and sub-directory then follow the below steps and please keep in mind that a directory must have an __init__.py file for python to recognize it as a package.

  1. First run this to see all paths being searched by python:
import sys
sys.path

You must be able to see your current working directory in that list.

  1. Now import the sub-directory and the respective module that you want to use via the import command: import subdir.subdir.modulename as abc You should now be able to use the methods in that module.

enter image description here

As you can see in this screenshot above I have one parent directory and two sub-directories. Under the second sub-directory I have a module named CommonFunction. On the console to the right you can see my working directory after execution of sys.path.

drapkin11
  • 1,205
  • 2
  • 12
  • 24
Gaurav Singh
  • 85
  • 1
  • 3
2
  • Does the models folder has an __init__.py file inside it ? Only then, it will be recognized as a module by python and import models would make sense.

So,

  • Create an empty __init__.py file in the models subfolder and then the code should work without any issues.

You should also look at this answer.

Community
  • 1
  • 1
Ujjwal
  • 1,849
  • 2
  • 17
  • 37
-2

if create d or using or custom python package

check our dir. correct.

** For python 3.7 user **

from. import module_name
-2

If you are using python3 but are installing the API through 'pip install 'such and such'. It's not gonna work on python3 console. Therefore, you'd better use 'sudo python3 -m pip install 'such and such''. Then it's gonna work! (at least for ubuntu stuff)

:)

Marlon Teixeira
  • 334
  • 1
  • 14