i want to remove my source code .py files to avoid rivals to see my code.
so i use
python -c "import compileall; compileall.compile_dir('D:/acc')"
to compile all py files to pyc,next step is to remove py files,and run the project only use pyc, how to do it?
you can use shutils, which is in the standard lib but please consider that removing *.py files is not a very string protection as it is very easy to rebuild the *.py file from a *.pyc.
Using py2exe with a drm packer would be much more secure.
One convenient approach is to rename the main module pyc file to __main__.pyc and put all pyc files in a zip file. Python 2.6 and above are then able to run the zip:
python myapplication.zip
Be aware that pyc files are not compatible between different Python versions.
Related
I have a question if I use Pyinstaller to convert python file to exe will convert the modules with or not? , because i have python file with a lot of modules and i want to convert it how i can do it and avoiding this issue,
Thank u.
import requests
error:
ImportError: No module named requests
.
After discussion in comments basically the error is occurring when you try to open the generated file in another computer, however your aren't using any virtual environment so you can install the requirements and try to rebuild again but rather you want a stanalone exe file.
For that use :
pyinstaller --onefile your-script.py
# or pyinstaller -F your-script.py
## this should generate a stand alone executable file located in the dist folder.
About your concerns on how pyinstaller works
Does pyinstaller make copies of modules when building ?
The answer is simply : yes , as mentioned in the docs here PyInstaller reads a Python script written by you. It analyzes your code to discover every other module and library your script needs in order to execute. Then it collects copies of all those files – including the active Python interpreter! – and puts them with your script in a single folder, or optionally in a single executable file.
However, the variations of Python and third-party libraries are endless and unpredictable, if something goes wrong you can learn how to fix those issues by reading this page on the docs here
What to generate ?
you can read more here
Create a one-folder bundle containing an executable (default), -D, --onedir
Create a one-file bundled executable. you need to use -F ore --onefile
Finally
I highly encourage you to use separate virtual environment for each project.
I have a Python directory with a number of .py files. I recently compiled them into .pyc files using python -m compileall. I have now changed some of the source files and would like to recompile, writing over the old .pyc files.
Is there a quick way to do this from the command line without having to manually delete all existing .pyc files?
You just have to run python -m compileall again. It will overwrite older .pyc files. You can pass it the -f switch to force rebuild even if timestamps are up to date (as per the documentation).
When the source code has changed, new .pyc files are automatically created when you run the program again. Therefore I wouldn't worry about compiling, but focus your attention on the code itself.. :)
The original file name is view.py, forms.py but python automatically adds/saves some file named view.pyc, forms.pyc
I want to delete those extra view.pyc and forms.pyc files.
Why do I get those .pyc files?
Or how can I tell python not to create those extra files with .pyc extension?
pyc files are compiled bytecode files, not backup files
6.1.2 ``Compiled'' Python files http://docs.python.org/release/1.5.1p1/tut/node43.html
As an important speed-up of the start-up time for short programs that use a lot of standard modules, if a file called "spam.pyc" exists in the directory where "spam.py" is found, this is assumed to contain an already-``byte-compiled'' version of the module spam. The modification time of the version of "spam.py" used to create "spam.pyc" is recorded in "spam.pyc", and the file is ignored if these don't match.
Update:
To avoid writing pyc files (Python 2.6+)
start with the -B flag (python -B)
set the PYTHONDONTWRITEBYTECODE environment variable.
See also How to avoid .pyc files?
Vim backups are named view.py~
Disable vim backups using
:se nobackup
.pyc files are compiled python modules created by the python interpreter; they have nothing at all to do with vim.
You can avoid them by invoking python with the -B flag or by setting the PYTHONDONTWRITEBYTECODE environment variable.
it's not vim, python compiles your py into that format to make things go faster.
I am a complete newb to python, hence a silly question.
As i understand, upon first execution of a *.py program, byte code is created into *.pyc and used until a change in *.py file.
Where might this *.pyc bytecode be found in a project?
I would think bin, but nothing is there
A *.pyc file is created for imported modules, and they are placed in the same directory containing the .py file. However... no .pyc file is created for the main script for your program. In other words... if you call "python myscript.py" on the command line, there will be no .pyc file for myscript.py.
This is how Python 2.x behaves. However, in Python 3.x the .pyc files are saved in a __pycache__ directory. See David Glick's answer for details.
[edit: Added note about Python 3.x.]
In Python < 3.2, the .pyc files are placed in the same directory as the .py file.
In Python 3.2, the compiled files are placed in a __pycache__ subdirectory, and are named differently depending on which Python interpreter created them. (This can be useful to people importing the same Python modules from multiple versions of Python.) See http://docs.python.org/dev/whatsnew/3.2.html#pep-3147-pyc-repository-directories for more information.
They are always created in whatever directory contains your .py files. Also, they are created for imported modules, not files that you directly run.
If you want to create .pyc files, boot up a Python interpreter and just import the modules of your choosing.
The .pyc files are normally created in the same directory as the files that they correspond to. E.g., both my_file.py and my_file.pyc should be in the same location.
I have have a python file that imports a few frequently changed python files. I have had trouble with the imported files not recompiling when I change them. How do I stop them compiling?
I don't think that's possible - its the way Python works. The best you could do, I think, is to have some kind of automated script which deletes *.pyc files at first. Or you could have a development module which automatically compiles all imports - try the compile module.
I've personally not had this trouble before, but try checking the timestamps on the files. You could try running touch on all the Python files in the directory. (find -name \\*.py -exec touch \\{\\} \\;)
There are some modules which might help you:
The py_compile module (http://effbot.org/librarybook/py-compile.htm) will allow you to explicitly compile modules (without running them like the 'import' statement does).
import py_compile
py_compile.compile("my_module.py")
Also, the compileall module (http://effbot.org/librarybook/compileall.htm) will compile all the modules found in a directory.
import compileall
compileall.compile_dir(".", force=1)
In python 2.6, you should be able to supply the -B option.
You are looking for compileall
compileall.compile_dir(dir[,
maxlevels[, ddir[, force[, rx[,
quiet]]]]])
Recursively descend the directory tree named by dir, compiling all .py
files along the way.