I want to set the default permissions for files and directories created within my Python script or any of its sub-processes. To this end, I call my script as follows:
umask 0137
python run.py
However, the directories created within Python end up to have the same permissions as the files, that is rw-r-----. Since they are missing executable permissions, one cannot open and browse the directories before changing its permissions first. On the contrast, files should not have executable permissions by default.
Is this possible?
Related
I've successfully added elevation to my pyinstaller-generated exe by using uac_admin=True in the spec file.
https://pyinstaller.org/en/stable/man/pyi-makespec.html?highlight=uac-admin#windows-specific-options
However, within folders created by the exe, one needs admin permission to create additional files and folders, while this is not true for the parent folder not generated by pyinstaller.
I've tried the method described here:
https://stackoverflow.com/a/12168268/1957737
To set the permissions of all of the subfolders to the permissions of the parent folder. That seemed to work when running the code as admin, but doesn't have any effect when running the pyinstaller exe - the folders still require admin permission to write to.
To clarify:
d:\parent - the parent folder
d:\parent\generated - a folder generated by pyinstaller
creating files and folders in d:\parent does not require admin
creating files and folders in d:\parent\generated does require admin
Thanks for any help.
I've created a python application for scraping local publications and identifying articles that contain certain keywords. Within the main application directory are contained a main script and several custom modules, which are imported by the main script. Also contained in this directory are sub-directories for configuration files, logging files, and output files. The configuration files (publications, keywords) and the output files (identified articles) must be accessible and editable by the user.
I would like to distribute this application such that users would not need to download python and the requisite dependencies. It seems to me that the best method for this is to create a standalone executable with a utility such as pyinstaller. However, I don't know how to do so while also retaining the configuration and output files as accessible and editable, as mentioned above. Any suggestions, including other methods of application design/distribution, are appreciated.
You can use pyinstaller in collaboration with NSIS to create an installer that transports zipped folders. This zipped folder can (and must) contain your newly created script.exe and all of its required dependencies. They would be accessible to the user as they will simply receive the entire folder. This video might be helpful: https://www.youtube.com/watch?v=UZX5kH72Yx4
I'm working on building my first Python package for a client to use. At the most, I am envisioning the user pulling the code from GitHub, then pip installing (pip install .). This package will be used in a Windows environment. What is the convention or where is the easiest place to put log files? Is there a way to tell setup.py to make a log directory that is easily accessible for the user?
For a more specific example, let's say I had the code base locally at C:\Users\iamuser\projects\client_project. I pip install . while in the client_project directory. There is a logs\ directory (C:\Users\iamuser\projects\client_project\logs) that I'd like the log files to be placed into. Is there a way to have my setup.py place log files in that directory? If not, are there any other tools I should try?
I have tried something like this, but any paths acquired while running setup are not where the original setup.py file was located (example: os.path.abspath(__file__) shows some other location than within the client_project directory).
While creating a Python package, I would not make any assumptions about the user's filesystem or permissions therein (ideally not even about the OS). If your Python package creates log files, you could use Python's build-in logging system and logging information would go wherever the user wants it to go (stdout and stderr by default).
If your package generates files the user should have the option to decide where they go, either using a settings or config file or environmental variables.
There are a few places where you could safely store them by default. Such as the home or current working directory (or subfolders of them, see pro and cons in the comments). Important is to use relative paths either in relationship to ~, os.getcwd() or the __file__ attribute of your script. Linux systems have a few places that are usually present and can be used such as /tmp or /var/log but that does not work on Windows.
Sometimes, I store output files in the parent of the current working directory in order not to checkin output files into a Git repo but this relays on additional assumptions.
I am trying to use a set of Python 2.7 scripts in a folder to process some *.csv files in another folder on Windows 7.
Here is the folder with the scripts C:\Users\UserH\workQ and its contents are
temp_far_cels.py
convert_press.py
farrug_py33.py
site_chordelerian_asym.py
Unfortunately, these scripts are part of libraries and I:
should not be editing them; I can only use them from the command prompt.
should not place the *.csv files in the same folder as these scripts
Here is the folder with the *.csv files
C:\Users\UserH\Downloads\bu59_files
According to this I need to set my PYHTONPATH
variable. I have done this form My Computer > Properties > Advanced > Environment Variables. Here is my PYTHONPATH variable
%PYTHONPATH%;C:\Users\UserH\workQ
I have Python in my PATH variable as
....;C:\Python27;C:\Python27\Scripts
Currently, I am in the folder containing the *.csv files and I am trying to run a script from the scripts folder with this
C:\Users\UserH\Downloads\bu59_files> python temp_far_cels -s 'cal' -t "March 2014"
However, I get the following error message
python: can't open file 'temp_far_cels.py': [Errno 2] No such file or directory.
Is something missing here or do I need another step somewhere to run these Python scripts from any other folder in Windows?
I don't have a windows machine to test on, but I see no reason why it wouldn't work this way:
python "C:\Users\UserH\workQ\temp_far_cels.py" (options)
I think you are over thinking this trying to work with the path files in this case.
I am a Python beginner and I am having trouble running Python from CMD. I have added the Python installation directory as a PATH variable (;C:\Python27). I am able to run the Python Interpreter from CMD, however when I issue a command like "python file.py command" from CMD, it returns "Error2, Python can't open, no such file/directory".
So what I do is go to "cd C:\Folder\Folder2\My_Python_Files", then type the "file.py command" each and every time. Is there faster or more efficient way of doing this? I am currently running Python2.7 on Windows 8.
When you run python <script>, it requires an actual path to the script being provided. You cannot specify "file.py" alone, unless it is right there in your current directory.
In windows, here are two steps you can take:
Associate .py files with python. Then you can run them directly without the python command as: /path/to/file.py
(right click a .py -> properties -> change to associate with python.exe)
Further step: Add a location to your PATH environment which will contain your python scripts. From there, you can just do file.py and it will be found in your search path.
So you could add C:\Folder\Folder2\My_Python_Files to your PATH and that is where you can store your executable python scripts.
Also you can set the PATH variable temporarily during a shell session:
SET PATH=%PATH%;C:\path\to\project
Just like PATH environment variable lists several directories for the system to search for executables, the PYTHONPATH do the same for Python to search for .py files. If you want scripts in a folder to be globally accessible (i.e. you can reference them by name just like you want, or you can import them from other scripts), add that folder to PYTHONPATH (create it if it doesn't exist).
Note that the command to invoke a script that is in your PYTHONPATH is:
python -m file [<script arguments>]
(i.e. use the -m option to treat it as a module, and don't use the extension .py)
Here's an article explaining in more detail how Python finds its source files (both in the command line and through import).
Note that you can also refer to the script by using its full path:
python C:\Folder\Folder2\My_Python_Files\file.py command
But by doing this, other files in the same folder that this script might reference through import might not work (since Python doesn't know where to search for them).
Unless the project's folder is in the PATH, you cannot call the file unless you are inside the project's folder. Don't create PATHs for projects, unless they are needed; it's unnecessary.
Just transverse to the file's directory and run the command inside the directory. That will work.
If the project will be used by other projects/files, you can use PYTHONPATH to set the directory, so the other projects can successfully access it.
Hope that helps.