A rookie question here:
I have a PY code and compiled it to create a .pyc. I would want to use this pyc file instead of PY.
I am running the PY file using external program. When the PY exists in the folder everything works perfect. However when I remove the PY file and simply use the pyc I get error:
IOError: [Errno 2] No such file or directory: 'E:/data/test/tech.py'
Whereas I have a tech.pyc lying around in the same folder.Any ideas what could be the issue here?
Normally, python is not compiled. The .pyc files are only a performance optimization that improve loading times.
Python is looking for the .py file because it always checks that first. If a .pyc file is newer than its corresponding .py file, it will then use the .pyc file. If the .py file is newer it will create a new .pyc file.
Related
I Created a Program That Has 2 .py files.
I Want To Make The Program a .exe file, I do it using cx_Freeze.
My Problem Is That I Convert The main.py To .exe but The Second Python File Is Still a .py File.
I Don't Want It Be a .py Because If It Is The User Can See The Code.
If I Also Convert The Second Python File The Program Doesn't Work Because I import The Python File In The Main File.
Any Suggestions?
(I Don't Want To Copy The Second Python File To The Main Python File)
You are chasing the wrong rabbit here. The various tools that generate executable files from Python code are not compilers. They are just tools that embed a Python interpretor with py (or pyc) files to allow users to use the program without a prior Python installation.
Said differently you should not use them to hide your code (except from people not knowing a lot of Python): a pyc does not contain text code but according to the answers to Is it possible to decompile a compiled .pyc file into a .py file? , tools exists that convert back a pyc file into a py file (of course except the comments).
IMHO, you should better google for python obfuscate to find tools dedicated to obfuscation, what neither cx-freeze nor pyinstaller are.
BTW while there are appropriate use cases for obfuscation you should be aware that a determinate attacker can always circumvent it. The reason why the real protection for intellectual property is law and not technics...
I'm not sure how two or more .py files can be converted to .exe.
But in python the easiest way to convert to .exe is a module named pyinstaller .
You can install it using command pip install pyinstaller can get it . After just go to the directory where your project files are and open command prompt in that directory and execute pyinstaller file_name
I created an application that reads a text file to get a digit (1 or 0) to execute some code (it runs like a save file since I want the value to be changed permanently, not just while the script is running), but when I use pyinstaller to convert it to a standalone exe file I get a Fatal Error saying that it cannot run the script. Previous compilations from before I added this feature worked, so I know it's something to do with reading that text file.
Note: I tried using a .py file instead of a .txt. While it worked as a script, the same error came up after compilation.
this is what I type into an elevated command prompt:
pyinstaller --onefile --add-data save.txt;.' file.py
You can not edit the data files that you have bundled with your application using --add-data in pyinstaller's --onefile method as it unpacks all the files into a temporary directory when executed, therefore the file wiil not be found in the current directory as the application demands, so you end up with the Fatal Error.
To get around this, refer to this post, use that function in your code and send your relative path into it, so as to get the absolute path of the file when the application is run. Remember, the changes that you make will be lost after you close the application, as these files are treated as "data files", they are not intended to be modified, so the binary will create a new copy of initial file on every application launch.
If you want it to permanently be stored somewhere, you can implement the file creation (if it doesn't exist) form the script itself, by creating a directory in the AppData folder and save the file there.
I have decompiled a .exe compiled.
I got a .pyz file and some other files:
Extracted them and got this:
How am I able to get the .py file from all those .pyc files?
You can decompile .pyc files yes - see these similar questions:
Is it possible to decompile a compiled .pyc file into a .py file?
https://reverseengineering.stackexchange.com/questions/1701/decompiling-pyc-files
The short of it is that you can use a tool to generate .py files from .pyc:
https://github.com/rocky/python-uncompyle6/
Currently going through a course on Python which is using 2.7. I have 3.5 installed and have been able to translate thus far. I'm having problems with the open() function. For example: see script and result below.
errno2
I have the file "textfile.txt" in the same folder as the .py file I'm running. As a check, I also put the file in the Python35 folder.. Any suggestions?
"same folder as the .py file I'm running" -- open() will search relative to your current working directory, regardless of the py source file's location.
From datamodel docs:
__file__ is the pathname of the file from which the module was loaded, if it was loaded from a file
You can use __file__ and os.path.dirname() to find the directory of the source file and from there look for the text file.
I seen the some difference when I execute the .py file. I have observed two cases,
1) when I run the .py file using the python mypython.py
I got the result. But .pyc file not created in my folder.
2) when I run the .py file using the python -c "import mypython"
I got the same result. But .pyc file was created in my folder.
My question is why first case not created .pyc file ?
Import is generally used when you need to use the contents of a file in another script or program, see What does python file extensions, .pyc .pyd .pyo stand for?. So to more specifically answer the question, the .pyc is created to ease access to the contents of the file in the future and is only created when the import command is used.
Python saves the precompiled .pyc file only for imported modules, not for the main script you're running.
Running a program as main or importing it as a module is not the exact same thing, but very similar because in a module everything that is at top level is executed at import time.
Note that for main program the source code is completely parsed and compiled too (so for example if you have a syntax error in last line nothing will be executed). The difference is only that the result of compilation is not saved back to disk.