Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Why would you want to import a module from a parent directory. Sounds like a design problem.
-
@stop I'm pretty sure I use from ..module import x,y the other day to import from a parent directory
-
purist12164yOh 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.. -
Hazarth95114yYeah, 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... -
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. -
crisz82364y@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
Related Rants
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
rant
javascript is better
python
it's fucking 2021
why