0

I am running the following simple script from the book and getting the following error

from mrjob.job import MRJob

class MRWordCount(MRJob):
 def mapper(self, _, line):
  for word in line.split():
   yield(word, 1)
 def reducer(self, word, counts):
  yield(word, sum(counts))

if __name__ == '__main__':
 MRWordCount.run()

Using cygwin64 on windows 10 it returns the following error:

User001@Ubuntu /cygdrive/c/Users/User001/PycharmProjects/TestProject
$ python preparation.py input.txt
Traceback (most recent call last):
  File "preparation.py", line 1, in <module>
    from mrjob.job import MRJob
ImportError: No module named mrjob.job

Here is what I did:

  1. I installed mrjob using pip install mrjob and it installed successfully.

  2. I have checked that files exist in site-packages/mrjob and job.py file also exist and I can open the file and see the methods inside that file.

  3. I am using Pycharm so when I try to import mrjob it also gives me syntax completion that is pycharm recognizes where is the file.

Now I do not understand why it is unable to get this module. Can anybody help ? Thanks

muazfaiz
  • 4,611
  • 14
  • 50
  • 88
  • 2
    Check if you have multiple python versions installed – redacted Apr 04 '17 at 08:51
  • Check if you have a local `mrjob.py` file or `mrjob` directory with an `__init__.py` file, masking the package. – Martijn Pieters Apr 04 '17 at 08:56
  • I recently installed windows so there is no other python version. I have the `__init__.py` and `mrjob.py` files too in the packages – muazfaiz Apr 04 '17 at 09:04
  • I figured it out. @Robin was right I had two versions of python (2.7). Though i didn't install it myself. I just installed cygwin64 and it was installed from there. So whenever I run python from cygwin it runs python 2.7 but when I run it from command prompt it runs correctly with python 3.5. – muazfaiz Apr 04 '17 at 11:21

1 Answers1

0

From The Module Search Path.

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

  • the directory containing the input script (or the current directory).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • the installation-dependent default.

Which means you should avoid naming your modules with the same name as standard-library or built-in module names. So you'd better rename your package name or script file name instead of mrjob.py.

McGrady
  • 10,869
  • 13
  • 47
  • 69