Problem definition:
I am running python scripts from Excel using xlwings and I have a file that I am using in these scripts.
I used to have the path specific to my computer (ex--> C:\users\myuser\some_folder\myfile).
now I am sending these scripts to my collogues to use, and I want the path of the file to be automatically configured according to the location of the file on their machines. I tried using os package.
I tried this post and this post and finally tried moving the file in the same location and used os.getcwd() to get the working directory and concatenated with the file name to get the path.
these methods worked fine when I am running the python scripts alone (not from Excel), but When I tried running from Excel it did not work because for some reason when running python scripts from Excel the working directory changes to C:\\ProgramData\\Anaconda3\\
and no longer sees the file. also, these method (according to my understanding) uses the path of the directory from which the file is running.
they are only seeing files in C:\\ProgramData\\Anaconda3\\.
my 1st thought was to try to search for the folder name using this solution but the problem is that I do not know which location the end user will store the folder in.
What I am thinking now is find a way to locate (form this location C:\\ProgramData\\Anaconda3\\ (where python is run from Excel)) the folder which the file is stored at and from there easily grab the file path. after searching the web I did not find a suitable solution for my case.
so, is there a way to do this using os or any other package?
__file__ contains the absolute path to the executed python script without being effected by the current working directory
# example script located at /usr/bin/foo.py
print(__file__)
output:
/usr/bin/foo.py
Related
I am attempting to use the package WhiteboxTools. I have installed it to my python environment and imported it to my Jupyter notebook script. The whitebox documentation suggests explicitly setting the path to the executable file if it is not in the same folder as the script being written with the following:
wbt.set_whitebox_dir('/local/path/to/whitebox/binary/')
Another reason I believe this to be the problem is that I set the path to this file in my first code chunk: wbt.set_whitebox_dir('C:\\Users\\maxduso.stu\\Anaconda3\\envs\\geo\\pkgs\\whitebox_tools-2.2.0-py39hf21820d_2\\Library\\bin\\whitebox_tools.exe'), but the error arises in the next code chunk which calls one of the tools:
wbt.breach_depressions_least_cost(
"C:\\Users\\maxduso.stu\\Desktop\\FCOR_599\\project_work\\data\\tif_folder\\full_pa.tif",
"C:\\Users\\maxduso.stu\\Desktop\\FCOR_599\\project_work\\data\\tif_folder\\sa_breached_dem.tif",
dist = 10, #maximum search distancefor breach paths in cells
max_cost=None,
min_dist=True,
flat_increment=None,
fill=True
)
[WinError 267] The directory name is invalid: 'C:\\Users\\maxduso.stu\\Anaconda3\\envs\\geo\\pkgs\\whitebox_tools-2.2.0-py39hf21820d_2\\Library\\bin\\whitebox_tools.exe'
whitebox_tools is one of two files I attempted, the other being whitebox_gui. These files were suggested to me when I searched for ".exe" in the whitebox tools folder within my environment folder. That said, they are type = Application and so that brings me to the question: why did windows autofill "whitebox_tools.exe" in the file search bar but find an application file? Also, considering I cannot find a file of type .exe, where do I go from here?
Note: the tools also don't run without the path set.
Thanks for reading. I hope the question was clear enough.
I did an application in Python using Pygame, then I did the exe using cx-freeze with build_msi option. The result is that the folder where I install the application contains in its root all files needed to run (apart the libs created by cx-freeze). So running my app from the folder where is installed or clicking its link on the desktop it works. Instead if I tr to start the application from a different folder using the console it does not start because it does not find some files, but it does not search for them in the folder where they are installed... so... the problem is that I need to start the application also from a different folder and I can not use a bat file where I could change to the folder and then run the application. Is there a way so that the Python application may know where are the files without fix their path on it (because in this case I would have a lot of problems during development)? I tryed to access them trough something like os.getcwd() + fileName but it does not work anyway.
The problem here is with the working directory because if you open the file from it's folder then the working directory will be correct but if you open the file from another place the working directory will be different and you will be looking for something that doesn't exist. os.getcwd() returns the working directory. but os.path.dirname(__file__) returns the directory to the folder that the running script is in. so you can do that:
import os
projectFolder = os.path.dirname(__file__)
os.path.join(projectFolder, fileName)
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
I have tried writing some content to a file by running a python code in a batch file.I have used the following as the batch command.
python C:\Python27\filtercsv.py
When I tried running the program in IDLE or in the command prompt, it was writing to the file. But, when I tried the same using the batch file, it wasn't writing.
I have included python in the PATH environment variable. So, I guess I don't need to specify the .exe file's location.
Can anyone tell me how to solve this issue?
Do you write the file to an absolute path?
If not you might want to either change the current directory in the batch file before running the script:
pushd C:\Python27
python filtercsv.py
popd
or include a path in the file name you're writing to.
This is because of if you write to a file just by name it will show up in the current working directory, which is not necessarily where the script is located.
(However, you should probably not save your scripts in your Python installation directory, nor write files there because you don't usually have write access there.)
I realise this question may already exist, but the answers I've found haven't worked and I have a slightly different setup.
I have a python file /home/pi/python_games/frontend.py that I am trying to start when lxde loads by placing #python /home/pi/python_games/frontend.py in /etc/xdg/lxsession/LXDE/autostart.
It doesn't run and there are no error messages.
When trying to run python /home/pi/python_games/frontend.py, python complains about not being able to find the files that are loaded using relative links eg: /home/pi/python_games/image.png is called with image.png. Obviously one solution would be to give these resources absolute paths, but the python program also calls other python programs in its directory that also have relative paths, and I don't want to go changing all them.
Anyone got any ideas?
Thanks
Tom
you could change your current working directory inside the script before you start calling your relative imports, use os.chdir("absolute path on where your script lives").
Rather than change your current working directory, in yourfrontend.pyscript you could use the value of the predefined__file__module attribute, which will be the absolute pathname of the script file, to determine absolute paths to the other files in the same directory.
Functions in theos.pathmodule, such assplit()andjoin(), will make doing this fairly easy.