Skip to content

Instantly share code, notes, and snippets.

Created Dec 14, 2016
What would you like to do?
This imports something from the parent dir of the script file in a very nasty way.
# This is some butt ugly fuckery that basically chdirs to the
# directory that is parent to this source file, imports the
# test subject from the main script, then de-imports (not really
# cause Python doesn't support that) the stuff that it needed to
# do that as not to pollute the name space and cause tests to
# possibly fail because of that.
# In order for this to work it's imperative that you:
# - Have the main script on the parent directory to the one
# this script file is in.
# - Don't use a weird naming scheme for things (slashes and stuff),
# don't abuse symlinks near this project, be wary about the file
# structure of the project.
# - Understand that even though you may have taken care of the
# above, it still may fail because this is some mind boggling
# hackery, and this kind of fuckery doesn't come with a warranty.
# I am not particularly proud of this code, but it works like a charm.
# Kind of like speeding on an empty highway... With malfunctioning brakes.
import inspect
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(
os.path.abspath(inspect.getsourcefile(lambda:0))), os.pardir)))
sys.dont_write_bytecode = True
from yourmodule import whateveryoulike
sys.dont_write_bytecode = False
del os
del sys
del inspect
# End of nasty section
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment