17
crisz
3y

I loved Python from when I wrote my first program up to I googled "how to import a module from a parent directory".

My love lasted 30 seconds

Comments
  • 0
    why should i need an relative import from the parent module?
  • 3
    does "./../something.py" not work?
  • 4
    Why would you want to import a module from a parent directory. Sounds like a design problem.
  • 2
    @theabbie relative imports are in the form:
    from . import x, y
    from .module import x, y

    its only possible to going down.
  • 2
    @stop I'm pretty sure I use from ..module import x,y the other day to import from a parent directory
  • 1
    @crow22498 i was wrong. its possible since 3.7
  • 2
    Oh my god.. i ran into the exact same issue a year ago. It is baffling how bad the question is answered on SO also. There are 1500 answers for seemingly similar questions and finally one answer works after wasting 15 hrs trying every other solution only to be told.. Nope it wont work that way you have to run it as a module to make it work..

    For people wondering why ..module wont work, python just says imports wont work above the base directory of a package. I still dont understand why it is that way..
  • 1
    That "javascript is better" tag 😂
  • 0
    @stop that's probably it yeah
  • 0
    @purist because nothing is higher than the root.
  • 1
    Yeah, that's a design problem on your part. In the time I was working with python I never once needed to import a standalone module from a standalone parent (though I believe there's an entire package dedicated to loading .py files)

    what you need is packages... then you can reach any parent or sibling directory from the root of the package easily

    afterwards, inside myModule2.py you can do

    from myPackage.myModule1 import whatever

    where "myPackage" is the root (also parent directory of myModule2.py) and myModule1 is the module located at the parent. "whatever" is whatever you're trying to import from it...
  • 6
    Why do you read SO when Python has documentation.

    SO is a stackpile of shit.

    Don't use it.

    It's shit.

    Read this.

    https://docs.python.org/3/tutorial/...

    It is version specific documentation. Which is good, cause Python and any language / software can change and especially module loading got some upgrade's.

    Now take a pen, a paper and read the documentation while making notes.

    If u feel offended, let me make clear that hopping into Python, taking random SO posts for granted and creating a Python Frankenstein of obsolete and outdated shit is no fun.

    Look e.g. at Poetry / Python.

    And the PEP for pyproject.toml support.

    Python has, nowadays, very good support for module loading, virtual environment, installing / handling module versions and stuff like that.

    But only if u take the time to read up the several pages, the PEPs and stop reading outdated, opinionated and mostly misleading SO shit.
  • 1
    @IntrusionCM I don't get offended because I just needed to make something up and working, but I didn't mean to use python in a serious way. I agree on what you said
  • 0
    @Hazarth yes in the end I solved like this, Pycharm helped me a lot
  • 1
    @IntrusionCM Python documentation can vary from great to hysterically bad.
  • 0
    sys.path.append
Add Comment