3

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).

Kurt Maurer
  • 425
  • 1
  • 5
  • 15
  • you need to create the function to handle this task, see solution here by [alan](https://stackoverflow.com/questions/1594827/cleaning-build-directory-in-setup-py) – sahasrara62 Apr 09 '19 at 22:55

1 Answers1

0

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.

Falk Schuetzenmeister
  • 1,497
  • 1
  • 16
  • 35
  • logfiles into current work dir is not very nice. Different invocations could leave logs scattered all over the filesystem, and maybe current workdir is not even writable by the user. Better is to put them under user's homedir somewhere. Or, just don't configure a file handler by default, make that "opt-in". – wim Apr 09 '19 at 21:45
  • Home directory is a good one, I like it (but there are edge cases too where a home directory does not exist). I also agree that opt in for file locations should be preferred. Your right current working directory can be all over the place in particular if set manually which I btw. really don't like. I use either paths relative to my scripts or absolute. – Falk Schuetzenmeister Apr 09 '19 at 21:53
  • Re-reading the post it seems that the user wants to have the log files withing the setup structure of the project itself which can be easily achieved while storing the logs relative to the script that generates them. The one thing I like about putting them into the current working directory is that the user would see that the scripts actually generates them even though it is not a good production solution (therefore customization). Not sure whether it is a good practice to write logs into the package folder. – Falk Schuetzenmeister Apr 09 '19 at 22:21
  • Not good practice, and not even possible in some cases. The installer (might be package manager) may have permission to write there but the app at runtime won't be able to and can crash out. And depends on all sorts of external factors like the umask. – wim Apr 09 '19 at 22:25
  • Your re-read is correct. I was doubting this being good practice as well, but I'm nonetheless trying to follow the specification. I thought I could grab the absolute path to the `logs` directory at installation since I know where it is in relation to `setup.py`. Unfortunately, during installation, `setup.py` is transported into some `...\Temp\AppData` directory or something, thus `__file__` gives a useless filepath, or at least one that is not easy to find for the average user. – Kurt Maurer Apr 09 '19 at 23:56