Python Forum
How do I set the path to my module one directory below my python 3.7.3 program? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How do I set the path to my module one directory below my python 3.7.3 program? (/thread-20152.html)



How do I set the path to my module one directory below my python 3.7.3 program? - deac33 - Jul-29-2019

How do I set the path to my module one directory below my python 3.7.3 program?

I was able to have success using
--- import sys
--- sys.path.append("DEAC/0-files")
--- import DeacModule
so I can get my work done.

My question is about an alternate approach found at:
https://python-forum.io/Thread-Basic-Modules-part-3
which shows this format
--- import dir1.dir2.mod
--- from dir1.dir2.mod import Klass

My code uses:
--- import DEAC.0-files.DeacModule

which, despite trying variations on the path, always gets this error:
----------
File "/Users/deacimac6/DROPBOX/A PGMG/PycharmProjects/DEAC/Modules.py", line 117
import DEAC.0-files.DeacModule
^
SyntaxError: invalid syntax
----------

With indenting it looks more like this (with spaces instead of "-")
----------
--File "/Users/deacimac6/DROPBOX/A PGMG/PycharmProjects/DEAC/Modules.py", line 117
----import DEAC.0-files.DeacModule
--------------------^
SyntaxError: invalid syntax
----------

With a minor name change I have also received this error:
ModuleNotFoundError: No module named 'DEAC'

Can you see what's wrong with my syntax?


RE: How do I set the path to my module one directory below my python 3.7.3 program? - Larz60+ - Jul-30-2019

you can set the base path as follows
import os
from pathlib import Path
# Make sure in source path:
os.chdir(os.path.abspath(os.path.dirname(__file__)))
homepath = Path('.')
onebelow = homepath / 'onebelowname'
# Also, if you want path created if it doesn't already exist then add
onebelow.mkdir(exist_ok=True)
# to create a file in that directory:
mynewfile = onebelow / 'mynewfilename'
# To open that file for writing text
with mynewfile.open('w') as fp:
    fp.write('whatever')