Python Forum
make sys.path.append auto detect - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: make sys.path.append auto detect (/thread-4707.html)



make sys.path.append auto detect - dwiga - Sep-04-2017

Hi,
I facing a problem coding in jython.
but, what I want to ask is about python.
I have to write this code on every file I've create

sys.path.append("D:\AvosLab\digital_scale\python")
but, the problem is my friends and I using git to build that app, so between my friends and I have different path of work.
so, how to make a code wich autodetect without changing the path every pulling project from git.

thank you in advance...


RE: make sys.path.append auto detect - SquareRoot - Sep-06-2017

Hi,

So D:\AvosLab\digital_scale\python is the directory where the python file is saved?

If so, you can get the directory where the python file is saved with

import os
print(os.path.dirname(os.path.realpath(__file__)))
Greetings


RE: make sys.path.append auto detect - dwiga - Sep-07-2017

(Sep-06-2017, 09:18 PM)SquareRoot Wrote: Hi,

So D:\AvosLab\digital_scale\python is the directory where the python file is saved?

If so, you can get the directory where the python file is saved with

import os
print(os.path.dirname(os.path.realpath(__file__)))
Greetings

yes, it is.
so, can I write it like this?

sys.path.append(os.path.dirname(os.path.realpath(__file__)))
but, what I get on jython is..

Error:
Exception in thread "AWT-EventQueue-0" Traceback (most recent call last): File "python/BPB.py", line 5, in <module> sys.path.append(os.path.dirname(os.path.realpath(__file__))) NameError: name '__file__' is not defined