How can I compile to a .pyc the current file by putting the command in the tool menu of Python??
Compile the current file
Collapse
X
-
I'm sorry i don't understand what you mean?Originally posted by pmstelHow can I compile to a .pyc the current file by putting the command in the tool menu of Python??
The .pyc file will be generated the first time you run a python program .... eg. if you have a program called "date.py" then to run it you type "python date.py" and you should now have a "date.pyc" file in your current directory.
I don't understand what you mean about the tool menu in python? Are you using IDLE? Are you on Windows/Mac/Linux etc.?
More info please -
OK I need to admit it, I was not clear.
1st - I have python 2.3.5 where I added Python Win #208
In the ineractive windows of PythonWin, like any other version, you type:
import py_compile
py_compile.comp ile( "your_script.py " )
Saying that, this command will create your pyc file.
Now, under pull-down menu "View" clicking Option then going on tab "Tools Menu" you can add a command line there which is gonna be added to your pull down menu "Tools". I would like to incorporate the command mention above (ie: import py_compile;py_c ompile.compile( "your_script.py ")
But I just don't know how I can retrieve the actual script name loaded in memory to compile it automatically without having to type all the time:
import py_compile
py_compile.comp ile( "your_script.py " )
I have tried ==> import py_compile;py_c ompile.compile( self ) and it returns me self is not define. So I am not exactly sure how to catch on the fly the name of my script.Comment
-
Perhaps this will help?
[code=python]
>>> import sys
>>> sys.argv[0]
'C:\\Python24\\ Lib\\site-packages\\wx-2.8-msw-unicode\\wx\\py \\PyCrust.py'
>>>[/code]
The first element of argv is always the program name (as if typed into a console) and is the absolute path... so after a quick find and slice you'll be good to go.Comment
-
That works good except for one little thing. When I do the compile manually in interactive window of PythonWin, it will compile only the current last save as file. Let say I have "test_A.py" and "test_B.py" open. If I do a save as of test_B.py then I do py_compile.comp ile("test_B.py" ) I will get my pyc file. If I do right away py_compile.comp ile("test_A.py" ), it will not work until I do a save as of "test_A.py" prior to compile it.
Now the sys.argv have work but it is not working for a second file. It works only with the first file loaded in PythonWin editor. No matter if I do a save as of another file, it will not work unless I close PythonWin and reopen PythonWin with the other file.
Any idea?? Still sys.argv work fine.Comment
-
I'm going back and forth in different module that I have built to add new function in it or create new one or just create a new module with new function. Those modules and function can't run stand alone in Python, they work with a specific detailing software.
So having 5 or 6 different module open, when I use save as, I need to compile them before publishing it in our detailing software database. So trying to get a shortcut to compile the last save as file will save me time. I can do that compile command many times per day. this is where is usefull for me.Comment
-
I finally found compielall. So in the option to set the shortcut that I mention on a previous post I have just entered the following:
This will compile all py file within the current directory specified inside the command ans all sub directory. Not to bad ;)Code:import compileall;compileall.compile_dir('c:\\my_python_dir\\dev_dir')
I still look to compile the last saved py file but at least I have a temporary workaround or permanent workaround. Who knows!Comment
Comment