How to handle relative python paths in travis test - python

I have a unittest that loads json file with simulate(config="../data/config/default.json") and locally it runs fine and tests are passed.
Then I pass it to .travis.yml with - python -m unittest tests.test_consistency and it cannot find this json file. It raises error:
FileNotFoundError: [Errno 2] No such file or directory: '../data/config/default.json'
why is that, am I missing something with relative paths?

It seems that this solution worked, so whatever local file I have file.json I changed it in the test files to:
CONFIG_PATH = os.path.join(os.path.dirname(__file__), 'file.json')
It makes the code harder to work with, though. I needed to overload all places where I load the files locally with
params.paths.G = os.path.join(root_path, params.paths.G) # graphml of a current .city

Related

'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()

Don't understand the why configparser can't find the config file

I'm using cofigparser in my python project. Directory structure is as following:
root/dir/data.py
root/dir/config.ini
data.py code:
config_parser = configparser.RawConfigParser()
config_file_path = "data/config.ini"
config_parser.read_file(open(config_file_path))
This works from local machine. But when Im trying to build it from Jenkins, output is:
> config_parser.read_file(open(config_file_path)) 19:09:04
> FileNotFoundError: [Errno 2] No such file or directory:
> 'data/config.ini'
After many attempts to resolve it I find out, that from Jenkins it works only if I include root to the path, so as I change it as following:
config_file_path = "root/data/config.ini"
It starts working on Jenkins. But - I have above error message on local machine now. So now Im in the situation, that if I can run my code from local machine, I have to change that path, and don`t forget to change it back, when Im commiting changes to git. Has anybody any idea, why is that? And how to write the path which will work on both?
Note1:
All the imports Im using in project are without root directory included, so for example:
from dir.dir2 import foo
If I include root dir, it never works from Jenkins.
Note2:
Both machines running Windows.

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)

Python absolute path with __file__ for a module called different ways

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

IOError: [Errno 2] No such file or directory: 'users.txt'

I am getting the above error when I use a webserver to run my code, however locally in the Terminal this works fine. I believe this must be to do with the path to the file working locally but not remotely. I have seen the solution on stackoverflow is to add the filepath like '/user/xxx/library/' etc, however is there a solution that allows this to be system agnostic? As in if I copy this directory to another location/server it will still work?
You can import os, it's built it to Python. You can get teh absolute path of the .py file this way:
import os
ROOT = lambda base : os.path.join(os.path.dirname(__file__), base).replace('\\','/')
Now you can simply do the following:
ROOT('users.txt')
It should return the absolute path.

Categories