Python Forum
How do I set the path to my module one directory below my python 3.7.3 program?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I set the path to my module one directory below my python 3.7.3 program?
#1
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?
Reply
#2
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')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  working directory if using windows path-variable chitarup 2 682 Nov-28-2023, 11:36 PM
Last Post: chitarup
  create a default path with idle to a specific directory greybill 0 846 Apr-23-2023, 04:32 AM
Last Post: greybill
  Import a module one step back of the path prathampatel9 1 1,036 Sep-21-2022, 01:34 PM
Last Post: snippsat
Question Setuptools CLI program ModuleNotFoundError after splitting up module into package Postbote 1 2,279 Nov-25-2021, 06:35 PM
Last Post: Postbote
  No Module found in other directory than source code [SOLVED] AlphaInc 1 2,004 Nov-10-2021, 04:34 PM
Last Post: AlphaInc
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,150 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  In put path and directory Led_Zeppelin 1 2,445 Apr-23-2021, 03:25 AM
Last Post: Larz60+
  OS module and file/directory recovery hikerguy62 1 2,127 Aug-02-2019, 06:42 AM
Last Post: Gribouillis
  Directory path gahhon 3 2,918 Mar-03-2019, 11:39 PM
Last Post: DeaD_EyE
  Directory path EOF Graham 8 4,607 Nov-28-2018, 10:13 PM
Last Post: Graham

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020