I am making a console app to download gcp objects using service account GOOGLE_CREDENTIALS, which is stored in json file. When i buddle my project using auto-py-to-exe to make it as an exe file. The json file comes into this folder. I want to hide this file from user. Is there any way to do so??
You can use os.chmod at the end of your setup script to make any file of the build directory read-only. For example:
import os
import stat
os.chmod(path_to_file, stat.S_IREAD)
And also to hide use below :
stat.FILE_ATTRIBUTE_HIDDEN this indicates The file or directory is hidden. It is not included in an ordinary directory listing. Refer this Link how to use file attribute Hidden.
Refer this SO for more information
Related
Does the credentials-sheets.json file and two tokens for drive and sheets need to be in the same folder as my python script, or can they reside somewhere else on my machine.
For example, I've got a test.py file on my desktop and ezsheets works correctly if the json file and two tokens are also on the desktop.
Do I need to copy/paste these files into the same folder as my python project, or is there somewhere else I can store them where ezsheets will find them? It seems kind of redundant to have to copy paste them every time, but I'm currently in my first coding class so perhaps I don't know best practices.
It's ideal if you can have the JSON file in the same folder, but not required. I'm unfamiliar with ezsheets, but I think it will work anywhere if you specify the correct file type. You should put the JSON file and the two tokens in the same folder (but not required).
For adding the file in the same folder, you can make use with the sys module.
import sys
path = sys.argv[0]
The path variable will have the script path. You can use this knowledge to determine the other files' path.
My python module needs to save some configuration files into a disk, is there a standard convention on where to save your module configuration files? one that you won't run into a permission error.
You can store these configuration file where ever you want (as long as your python script have the write permission to this directory and no process prevent it from writing to this directory (is not locked by other process)), as long as your module can reach them for parsing or for import if they are a python files.
what are the options:
store them in the same module directory, and import/parse them.
store them in deferent directory and in your python module change the os.path with os.chdir(path) to this dir for parsing.
store them in dir_x and provide the dir_x path to your python script via command line args with sys.argv[1]. or with argparse!
I have written code in python which opens up a template excel file. Everyday, a midnight, it copies that template into a new excel file where the code will record data into it for that day. My goal is to create a single executable file containing my code AND the template excel file using pyinstaller.
Basically I want to be able to open the template excel file regardless of whether the computer contains the file or not by bundling the excel file into the exe file obtained from pyinstaller:
Right now I open the excel file as shown below:
import os
import openpyxl
theFile = openpyxl.load_workbook(os.getcwd()+"\templateExcel.xlsx")
currentSheet = theFile[theFile.sheetnames[0]]
However, when I include the excel file to the pyinstaller command as --add-data "templateExcel.xlsx;templateExcel.xlsx, and run the exe file, it is not able to detect the location of the templateExcel file. I understand that by running on a different computer, the os.getcwd() gives a different path so it would obviously not be able to open the excel file. Hence I needed a way to bundle the excel file into the exe file such that the python code can find it regardless of the computer.
You can use;
Adding a data file in Pyinstaller using the onefile option
Summarly:
pyinstaller --onefile --nowindow --add-data text.txt;included winprint.py --distpath .
and sample python script:
import os
import sys
os.chdir(sys._MEIPASS)
os.system('included\\text.txt')
Please note:
When using "getcwd()" you won't receive the desired outcome you were looking for, it will get you the place from which you have executed your exe file.
For example, look at the following long module:
from os import getcwd
print(getcwd())
If you execute it from different locations, you will get different results:
Had PyInstaller worked like you assume it would (placing the file relative to some module) you could have used file instead of getcwd() as it indicated where the module resides (which is constant, unlike the location from which the exe is executed), but it doesn't.
Any way, #Sezer BOZKIR's answer should suffice, note the comment I added there.
I recently compiled my first python program into a standalone executable. There is one problem however. My python program originally accessed a .json file within the same directory and wrote contents to it. Now that I have made my python file into an exec file, it does not seem to access the .json file in the same directory anymore. Please help!
I guess the problem occurs because the the executable is not in the same folder as your script.
I can suggest you this solution:
import os
# csfp - current script folder path
csfp = os.path.dirname(os.path.realpath(__file__))
json_file = os.path.join(csfp, "config.json")
And you do whatever you want to do with this json_file variable, that contains the path and the name of your json file (example C:\Some_folder\My_python_project\config.json)
Is it possible to tell the Django development server to listen to changes on some files (not the regular Python files in the project, they already creates a reload) and reload if these files have changed?
Why do I want this? When my gulp-script builds the JS and CSS, it takes all files and appends a hash to them. My working file frontend/css/abc.css will be built to build/css/abc-hash123.css. This so that browsers are forced to reload the content if anything has changed since last deploy. When this builds, a manifest file is created that contains the mappings. In the settings in Django, I read this manifest file at start and get all these mappings (from this example here). If I change a file, it gets a new hash, and a new manifest is written. The mappings currently loaded into the memory are then wrong, and thus I need to force a reload of the development server to get the new mappings.
Clarification: I got comments on using a VCS. So just for clarification: I use a VCS and check in my abc.css. abc-hash123.css is automatically built as a part of the build system.
As far as doc goes this is not supported.
This is a bit hacky, but just touching (or changing the timestamp in any other way) some .py file or some other file that development server observes (e.g. settings.py) in your build will do the job.
Also as comments suggest, versioning is better left to a VCS.
A bit of a hacky way, but I found a way to make force the Django server to reload on changes to the build directory. After some research on the Django autoreload module, I found that it only listen to changes on Python files, from modules that are loaded into sys.modules. So, I only needed to make the build directory a Python module:
In my build script (gulp), after I've built my files, I added a step to create the __init__.py file, to make the build directory a module. Then I also wrote a comment in this file that contains a time stamp, making it unique for each build:
fs.writeFile('./build/__init__.py', '#' + Date.now())
And then in my local.py (the settings to use locally), I simply import this new module, so it appears in sys.modules:
try:
import build
except ImportError:
print("Could not import from build folder. Will not auto reload when building new files.")