Reliably set working directory for python application - python

I am making a small application that I will invite some users to test for me. I have been developing in PyCharm, and running the program from the dev directory, and the issue of a working directory just recently came to my mind.
The program and all files are contained in the project directory. Naturally, I want the working directory to be the project directory, regardless of where the user decides to store the program.
What is the most reliable way of achieving this? I was thinking something like this
import os
dir_work = os.path.dirname(os.path.abspath(__file__))
and then set all paths relative to dir_work. Is there a better way? I have also been thinking of making a setup script that the user can edit, and that when run hardcodes some stuff into the source code. But if my problem can be achieved without user intervention, that would definitely be the best solution.

Related

How do programs know where their files are? And how to implement the same thing in python?

I am working on a python project that depends on some other files. It all works fine while testing. However, I want the program to run on start up. The working directory for programs that run on start up seems to be C:Windows\system32. When installing a program, it usually asks where to install it and no matter where you put it, if it runs on start up, it knows where its files are located. How do they achieve that? Also, how to achieve the same thing in python?
First of all, what do you mean by "their files"? Windows applications can store "their files" in multiple places (including but not limited to %CommonProgramFiles%, %ProgramData% and %AppData%).
That being said, the common location for simple applications and scripts is to use the same directory as the .exe (or script).
In Python there seems to be multiple ways to find this path, this seems to work nicely:
import os
print(os.path.abspath(os.path.dirname(__file__)))
See also:
How do I get the path of the Python script I am running in?
How do I get the path and name of the file that is currently executing?
If you plan to consume local files that contain raw data or processed data, defining a default directory or a set of directories can simplify your implementation, for example:
Place your data files under a specific set of folders in C:\ or place your files under the F:\ folder, that can be a part of your on premisses file system
Based on where your Python application is located, you'll need to use relative paths or a library to help you to locate these files.
Here are some examples:
os.path
pathlib

How to deal with working on one project on different machines (paths)?

This is my first time coding a "project" (something more than solving exercises in single files). A number of my .py files have variables imported from a specific path. I also have a main "Run" file where I import things I've written in other files and execute the project as a whole.
Recently I've started working on this project on several different machines (home, work, laptop etc) and have just started learning how to use GitHub.
My question is, how do I deal with the fact that every time I open up my code on a different machine I need to go around changing all the paths to fit the new machine, and then change them back again when I'm home? I started writing a Run file for each location I work at so that my sys.path commands are ok with that machine, but it doesn't solve the problem of my other modules importing variables from specific paths that vary from machine to machine. Is there a way round this or is the problem in how I'm setting up the project itself?
In an ideal world it would all just work without me having to change something before I run, depending on the machine I'm working on, but I don't know if that's possible.
My current thoughts are whether there is some command I'm not aware of that can set variables inside a .py file from my main Run.py file - that way I can just have a run file for each machine.
Any suggestions are gladly taken! Whatever it is, it must be better than commenting back in the correct file path each time I open it on a different machine!
You should always use relative paths, not static which I assume you have got.
Assuming your in an index file and you need to access images folder, you probably have something like /users/username/project/images/image.png
Instead you want something like ../images/image.png, this tells your index file to go backwards one folder to say the root of the project, then proceed into our images folder etc.
Relative paths mean you create a path from where your file exists, and not an entire path from ground up.
You need to identify all your setting that are hardcoded in your project, and make them relative to your current workspace. This is a common problem.
Also if you are using python, make sure that you do not hardcode the path to files in string, but instead that you use the proper system api:
os.path.join('file','to','your','ressource')
that will be computed differently if you are on unix or windows.

Make a Python app package/install for Mac

I have developed an application for a friend. Aplication is not that complex, involves only two .py files, main.py and main_web.py, main being the application code, and _web being the web interface for it. As the web was done later, it's kept in this format, I know it can be done with one app but not to complicate it too much, I kept it that way. Two two communicate with some files, and web part uses Flask so there's "templates" directory too.
Now, I want to make a package or somehow make this easier for distribution, on a OSX system. I see that there is a nice py2app thingy, but I am running Windows and I can't really use it since it won't work on Win. I also don't know will py2app make problems since some configs are in text files in the directory, and they change during the runtime.
So, I am wondering, is there any other way to make a package of this, some sort of setup like program, or maybe some script or something? Some simple "way" of doing this would be to just copy the files in the directory in the "Documents", and add some shortcuts to the desktop to run those two apps, and that would be it, no need for anything else. DMG would be fine, but not mandatory.
I believe what you are looking for is to add: #!/usr/bin/python to the first line of your code will allow your friend to just double click on the file and it should open. Just as a warning osx does not tell us what version and as such what version of python and what standard libraries are going to be present.
Also, just make sure that if they have played around with their settings to much and they double click on python it does not work they will have to choose to open the file in "terminal.app" in the Utilities Applications folder (/Applications/Utilities/terminal.app)
The other idea is borrow a mac and compile it with the py2app program that you already mentioned. Otherwise there is no generic binary file that you will be able to compile in windows and have run on mac.

Opening various main.py files in Eclipse without creating a new project

I have a python project developed in eclipse. Independantly there are various data directories in various locations and it is desirable to operate on each with a different main.py that imports the python project. If I store a main.py with my data, how can I open it in eclipse and run the debugger?
I can run ipython in each directory without a problem; but, I am not clear on how to move between different main.py files within eclipse. The run configuration does allow you to go outside the project and I would prefer not having to change this each time anyway. Any insight would be great!
You could do this by setting up each main.py + data directory as its own Eclipse project, each with a custom run/debug configuration. You do have extra projects, but they don't require any ongoing maintenance.
After the initial setup, you continue work on the real python project and debugging any given data directory consists of selecting the appropriate project and invoking the debugger. Not maximally elegant, but not bad.
If you're truly loath to make new projects, you could bring the main.py files into the project and store the location of the associated data directory inside each of them. Each main begins by changing to the data directory, and then runs as usual. In eclipse you set up multiple run configurations, one for each main.py which you can then run without any extra tinkering.
It is inelegant and fragile to keep the data directory information inside each main.py, but maybe better for your purposes.

Locking on file locations in a large Python project

When a Python project gets big, certain code segments, such as utility functions, tend to be run from various locations:
from a __main__
from a django server process
from a test in a test suite
In each case the working directory for the python interpreter may be different and assuming the project spans over a sub-directory tree, the following line doesn't always work:
with open('some_file.xml','r') as my_xml:
It doesn't work because some_file.xml isn't always in your working directory. You need to be specific regarding the file's location, however, the project may be deployed in various environments so simply adding the directory to the open statement isn't a good solution.
What would be an elegant and efficient way to "lock on" the location of the file throughout the project?
using the following variable to get the directory of the project may help
__file__
How to make a python program path independent?

Categories