1

I have read how pythons module system works, but it makes things IMO too formal because I must access everything from some script in the root package which makes things a little harder to deal with IMO. I am not making packages I want to publish with a nice API, but just loose conglomerations of scripts for my own research...

So I want to have everything work by being able to relative import things from parent directories (or maybe parents of parents) and still be able to call scripts from within the child directories.

project/
  dir/
    child/
      script1.py
  dir2/
    script2.py
  utils/
    utils.py

I want to be able to import utils from script1.py and be able to directly call script1.py without having to set up a traditional project. Is there a way to do this?

Joff
  • 11,247
  • 16
  • 60
  • 103
  • Does this answer your question? [Importing modules from parent folder](https://stackoverflow.com/questions/714063/importing-modules-from-parent-folder) – MrJuicyBacon Nov 14 '19 at 02:02

1 Answers1

0

Actually you can add whatever you want to environment variable PYTHONPATH. All the modules under the path can be accessed. For example, in your snippet, you can set PYTHONPATH=/path/to/project, then you can access modules by import utils.tuils or import dir2.script2.py. Somehow like this.

Sraw
  • 18,892
  • 11
  • 54
  • 87