Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to "compile" python

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34672
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since Steve wants something to research, I have a question I've been thinking about looking at.

The question is whether there is a way to "compile" Python to check the syntax and method calls are valid without running it? In particular when it calls other libraries.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, I think when you import a Python module, it compiles it to Python byte code immediately (and produces a .pyc file). So from an interactive Python command line you could import your module, then check for the .pyc file. This may be implementation specific though.

Second, there is a py_compile module which lets you compile your python script without executing it. So again from an interactive command line, you could do:

To produce the compiled .pyc files. I think you can also run it from an OS command prompt like:

to compile multiple files at once.

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:The question is whether there is a way to "compile" Python to check the syntax and method calls are valid without running it? In particular when it calls other libraries.

As for what compiling does - it makes sure the compiled code has proper syntax, but it does not process method calls, so I don't think it will be able to double check method call parameter counts, etc... those types of errors will pass through to run time still. But the syntax errors should get caught.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34672
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A high school team wrote a wrapper to avoid this problem. It makes sense there isn't something more generic. Unfortunate though. Not everything can be unit tested.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18218
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The whole point of late-binding languages like Python, Perl, Ruby, et. al., is that you don't have to have everything nailed down until the very last moment. You just "Git 'er Dun!" and worry about the consequences later. If you'd prefer to be humiliated, before you place the code into production, the best way to do that is to employ an early-binding language such as Java. Or COBOL. Or something like that.

Not to say that you can't try, but the community as a whole has other goals, so you're sort of swimming upstream.

Actually, it's the lack of strong typing that usually gets me, and that really can't be compensated for in a compiler or even in unit testing, since testing would only demonstrate success/failure for the objects you submitted for test, not stuff coming in from Left Field the way it does in real life.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34672
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:The whole point of late-binding languages like Python, Perl, Ruby, et. al., is that you don't have to have everything nailed down until the very last moment. You just "Git 'er Dun!" and worry about the consequences later. If you'd prefer to be humiliated, before you place the code into production, the best way to do that is to employ an early-binding language such as Java. Or COBOL. Or something like that.

Not to say that you can't try, but the community as a whole has other goals, so you're sort of swimming upstream.

Actually, it's the lack of strong typing that usually gets me, and that really can't be compensated for in a compiler or even in unit testing, since testing would only demonstrate success/failure for the objects you submitted for test, not stuff coming in from Left Field the way it does in real life.

I agree. I think Java is a better choice than Python for this project. The experiment with Python is for the students to show that Python makes development "faster." I buy that for prototyping stuff. For the "production" robot where it blows up in competition because you are calling a method that doesn't exist on code that hasn't been run before, this seems like a sure way to lose a match.

High school students are better about worrying about the consequences later than I am. Hence looking at ways to mitigate it.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18218
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
High School students think about consequences?

Good luck with that. You end up with the hacker mentality. Oh, we can fix that! (repeat, ad nauseum.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34672
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:High School students think about consequences?

Sure. After the problem occurs.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic