Python absolute path with __file__ for a module called different ways - python

In my app I have a setup python script (/root/ha/setup.py) which looks through a directory called modules and runs setup.py in each of the subdirectories.
The relevant code is this:
""" Exec the module's own setup file """
if os.path.isfile(os.path.join(root, module, "setup.py")):
execfile(os.path.join(root, module, "setup.py"))
The thing is I want /root/ha/modules/modulename/setup.py to work no matter where it's called from.
If I am in modules/modulename and run python setup.py it's fine but if I run it from the directory above modules/ i get this error
idFile = open(os.path.dirname(os.path.abspath(__file__)) + "/id.txt", "r").read()
IOError: [Errno 2] No such file or directory: '/root/ha/id.txt'
as you can see it is getting the path of the script that is calling it instead of the script that is running. It should be trying to read /root/ha/modules/modulename/id.txt
I've tried using different methods to get the path but all end up with this error...

execfile does not modify the globals (as __file__) so the exectued script will indeed take an incorrect path.
You can pass global variables to execfile so you can modify its __file__ variable:
script = os.path.join(root, module, "setup.py")
if os.path.isfile(script):
g = globals().copy()
g['__file__'] = script
execfile(script, g)

If what you need is to access a file from some of your packages, then consider using pkg_resources as documented here: http://pythonhosted.org/setuptools/pkg_resources.html#basic-resource-access
Example of getting content of a file stored as part of package named package is in this SO answer

Related

How to call python script in different directory using subprocess?

I have the following code to run a program located at
/Users/*/Downloads/Mood Script Static and another located a folder deeper at /Users/*/Downloads/Mood Script Static Edit/LowMood_Static. Now I am trying to invoke a subprocess controller that will run two python scripts, like this:
subprocess.call(['python3', 'SubjectSFbalance.py'])
print("\n")
time.sleep(0.5)
subprocess.call(['python3', 'LowMood_Static/LowMoodZScoring.py'])
However, when running the 2nd process, it correctly finds the .py script, but pandas cannot read the .csv files that are given in the LowMoodZScoring.py file, which is this:
Normalized_Subj = pd.read_csv('../SF_Output_Files/finished_sf_balance.csv')
giving the following error:
FileNotFoundError: [Errno 2] No such file or directory: '../SF_Output_Files/finished_sf_balance.csv'
However, when I run LowMoodZScoring.py without using the subprocess, I get no such error. What in subprocess is causing this error?
At first glance, since those CSV paths are relative, it looks like you need to add cwd="LowMood_Static" to the parameters of your subprocess call, so that it will run with that directory as the working directory.
To avoid this in a more comprehensive/future-proof way, it would be even better to have your LowMoodZScoring.py script load the CSV files relative to the directory of that script (i.e. os.path.dirname(__file__)), instead of relative to the current working directory.

Run Python Script From Script Directory/Current Directory

[Introduction]
Hi! I'm working on a python script to automate installation of UWP-Apps, it's been a long time i'm not touching Python; until this day. The script uses Depedencies inside the script directory, i've looking up on my older scripts and found this specific code.
os.chdir(os.path.dirname(sys.argv[0]))
[Problematic]
However, using the above code doesn't work on my current script but it's working fine on older scripts. When using above, it shows:
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: ''
Already looking up on Internet about this topic; but most of them was talking about running the script from outer/different directory that leads me to dead end.
Any helps is appreciated :)
The easiest answer is probably to change your working directory, then call the .py file from where it is:
cd path/to/python/file && python ../.py
Of course you might find it even easier to write a script that does it all for you, like so:
Save this as runPython.sh in the directory where you're running the python script from, is:
#!/bin/sh
cd path/to/python/file
python ../script.py
Make it executable (for yourself):
chmod +x ./runPython.sh
Then you can simply enter your directory and run it:
./runPython.sh
If you want to only make changes to the python script:
mydir = os.getcwd() # would be the folder where you're running the file
mydir_tmp = "path/to/python/file" # get the path
mydir_new = os.chdir(mydir_tmp) # change the current working directory
mydir = os.getcwd()
The reason you got an error was because sys.argv[0] is the name of the file itself, instead you can pass the directory as a string and use sys.argv[1] instead.
import os
from os.path import abspath, dirname
os.chdir(dirname(abspath(__file__)))
You can use dirname(abspath(__file__))) to get the parent directory of the python script and os.chdir into it to make the script run in the directory where it is located.

'No such file or directory' error when using nosetests

I have a little Python project of which I recently made a Conda package. Making the package was a pain on its own, however, I recently started adding tests to this using nosetests, which made it even harder.
To introduce: my package takes a certain input, performs a lot of (quantum chemical) calculations and then generates a folder in the same directory as the script which calls the package, containing the output data (some .png files, .txt files and binary files)
Using nosetests, I would like to check whether these output files are how they should be. I created a Python test script (using unittest) which creates the input and calls my package. Next, it imports the created file and the test file. However, this is where it goes wrong. I get the error that this file does not exist:
FileNotFoundError: [Errno 2] No such file or directory: 'results\\output.txt'
The directory looks like this:
project-path\
- tests\
- test-script.py
- results\
- output.txt
I call nose by running this in Anaconda prompt:
project-path> nosetests tests
And I import the file in the Python test script using:
result_file = open('results\\output.txt', 'r').read()
Does anyone know what goes wrong here? I think it has to do with the fact that the tests are executed in a test environment. In that case: how do I import my files?
Get the absolute path to output.txt, it is indeed the most reliable way to locate and open it.
import os, sys
basedir = os.path.dirname(sys.argv[0])
filename = "output.txt"
path = os.path.join(basedir, "results", filename)
result_file = open(path, 'r').read()

Unable to obtain actual directory for __file__ Python

I've been trying to get the location of an open .py file (using Spyder) that will be used to link various files and use the __file__ name so that any machine can run from its current directory.
The problem is that when I try:
location = os.path.abspath(os.path.dirname(__file__))
It gives me the error:
NameError: name '__file__' is not defined
I bypassed this by calling on the file as a string rather than the name file variable using:
location = os.path.abspath(os.path.dirname('__file__'))
This worked for getting me the parent directory of the file but not the actual folder that the file was in:
location = 'C:\Users\......\Scripts'
When really the location is in:
location = 'C:\Users\......\Scripts\ISO'
I've tried various combinations of abspath and dirname and realpath to get the true directory that the file is in but I cannot get it.
What am I doing incorrectly?
Because I was running the script inside the Spyder shell it did not output the proper working directory because the working directory settings were somehow misaligned with the file's path.
This can be solved by Run -> Configuration per file -> The directory of the file being executed
But the more effective way of running the script using __file__ is to run the script from the python3.X.exe file in the directory which the script is located.

Call a file in another folder in Eclipse for Python project

I have a small enough Python project in Eclipse Neon and keep getting the same error and can't find proper documentation on how to solve. In my main I need to call a file that is located in another folder. The error I receive is IOError: [Errno 2] No such file or directory:
I have an empty init.py file in the folder (XML_TXT) that I'm trying to use.
It looks like Groovy is importing okay, or else you would get an ImportError. An IOError indicates that it can't find "test.txt". Does that file exist?
It will work if the file path is relative to where you are running the script from. So for example if test.txt is in a folder
Groovy("folder_name/test.txt")
You can also go up in the directory structure if you need to, for example
Groovy("../folder_name/test.txt")
Or, if you want to be able to run the file from anywhere, you can have python work out the absolute path of the file for you.
import os
filename = os.path.join(os.path.dirname(__file__), 'folder_name/test.txt')
u = Groovy(filename)

Categories