Trying to make PLY work for the first time

2.2k views Asked by At

I'm new to Python and I'm having some problems trying to make PLY works. For now, all I want is to successfully run the example from the PLY homepage.

At first I tried to just download PLY-3.8, put the ply folder in the same directory I saved the example (calc.py) and ran it. The calc.py file is at the C:\Users\...\Python directory and the ply folder is the C:\Users\...\Python\ply, just to make it clearer. But I got an ImportError: No module named 'ply'.

Then I searched for a while, tried to update something called distutils and install the modules through the Windows PowerShell and so on and so forth, but none of that worked and I just reset the whole thing (reinstalling Python and all of that). But then I finally got it to work by simply inserting into the sys.path the directory path where the script I was running (edit: in interactive mode) was, by doing this:

import sys
sys.path.insert(0,'C:\\Users\\ ... \\Python')

This fixed the ImportError but, and this is where I am now, there are a bunch of other errors:

Traceback (most recent call last):
  File "C:\Users\...\Python\calc.py", line 48, in <module>
    lexer = lex.lex()
  File "C:\Users\...\Python\ply\lex.py", line 906, in lex
    if linfo.validate_all():
  File "C:\Users\...\Python\ply\lex.py", line 580, in validate_all
    self.validate_rules()
  File "C:\Users\...\Python\ply\lex.py", line 822, in validate_rules
    self.validate_module(module)
  File "C:\Users\...\Python\ply\lex.py", line 833, in validate_module
    lines, linen = inspect.getsourcelines(module)
  File "c:\users\...\python\python35\lib\inspect.py", line 930, in getsourcelines
    lines, lnum = findsource(object)
  File "c:\users\...\python\python35\lib\inspect.py", line 743, in findsource
    file = getsourcefile(object)
  File "c:\users\...\python\python35\lib\inspect.py", line 659, in getsourcefile
    filename = getfile(object)
  File "c:\users\...\python\python35\lib\inspect.py", line 606, in getfile
    raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module '__main__'> is a built-in module

Now I have absolutely no idea what to do. I tried to search for a solution but had no luck. I appreciate if anyone can help me out.

I'm on Windows 10, using Python 3.5.0 and iep as my IDE (www.iep-project.org) if these informations are of any importance.

In short: I just want to successfully run the example from the PLY homepage and then I think I can figure out the rest.

EDIT: I found out that if I do:

import inspect
inspect.getfile(__main__)

I get the exact same (last) error from before:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "c:\users\...\python\python35\lib\inspect.py", line 606, in getfile
    raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module '__main__'> is a built-in module

I think this is the culprit, but I still don't know how to fix it.

EDIT 2: I got it to work and answered the question explaining how, but if someone have a more complete answer, I would love to hear it.

1

There are 1 answers

0
pvbernhard On BEST ANSWER

To anyone having this problem, I found what was the issue. I still don't know why exactly is like that, so if anyone have a more complete answer to provide I would appreciate (I'm still a newbie at Python).

Anyway, it seems this code can't be executed in Interactive mode, it needs to be executed as a script. To do that on IEP it's Run > Run file as script or Ctrl+Shift+E. On IDLE you need to Open... the file (Ctrl+O) and then Run Module (F5).

As to why it can't be executed in Interactive mode, here's a little bit about the difference between interactive mode and running as script from the IEP wizard:

Interactive mode vs running as script

You can run the current file or the main file normally, or as a script. When run as script, the shell is restared (sic) to provide a clean environment. The shell is also initialized differently so that it closely resembles a normal script execution.

In interactive mode, sys.path[0] is an empty string (i.e. the current dir), and sys.argv is set to [''].

In script mode, __file__ and sys.argv[0] are set to the scripts filename, sys.path[0] and the working dir are set to the directory containing the script.

That explains a bit about why the inspect.getfile(__main__) was throwing an error: the __main__ had no attribute __file__. And also why I had to insert the current directory into sys.path: sys.path didn't had the current directory in interactive mode.

I hope this helps someone.