Git Product home page Git Product logo

py-imports-demo's Introduction

py-imports-demo

this was created only for demonstration purposes related to CYB3RMX/Qu1cksc0pe#58

when you run main.py, you should get:

C:\SOMEPATH\py-imports-demo>python main.py
Running main.py
Running target.py, with __name__: mod.target

when running target.py directly:

C:\SOMEPATH\py-imports-demo>cd mod
C:\SOMEPATH\py-imports-demo\mod>python target.py
Running target.py, with __name__: __main__
Traceback (most recent call last):
  File "C:\SOMEPATH\py-imports-demo\mod\target.py", line 3, in <module>
    from .utils import stub
ImportError: attempted relative import with no known parent package

importing the utility module as .utils when running with __name__ mod.target works, because python knows we are inside the mod package, therefore .utils must refer to the local mod.utils module. However, as you can see, when running the target module directly, you'll get an error. This is caused by the fact that when running this way, Python isn't aware it's running in a package at all (as evidenced by the value of __name__). That arguably makes sense to some degree, but has quite annoying implications. AFAIK, there is no way to craft location-agnostic imports (without multiple attempts and catching ImportErrors along the way) OR stooping to sys.path hacks. https://stackoverflow.com/questions/16981921/relative-imports-in-python-3.

this also works, but you probably wouldn't want to force invocations to adhere to this scheme:

C:\SOMEPATH\py-imports-demo>python -m mod.target
Running target.py, with __name__: __main__

py-imports-demo's People

Contributors

luis261 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.