Guest User

Untitled

a guest
Oct 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. # in main.py
  2. try:
  3. # An import that works if the current module is installed with pip
  4. from .otherfile import some_func
  5. print("The .otherfile import worked")
  6. except ImportError:
  7. # An import that works of I run ./main.py from my source code folder
  8. print("The .otherfile import failed")
  9. from otherfile import some_func
  10. print("The otherfile import worked")
  11.  
  12. [username@localhost mymodule]# python3.6 mymodule/main.py
  13. The .otherfile import failed
  14. The otherfile import worked
  15. Hello World?
  16.  
  17. [username@localhost mymodule]# python3.6
  18. Python 3.6.5 (default, Apr 10 2018, 17:08:37)
  19. [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
  20. Type "help", "copyright", "credits" or "license" for more information.
  21. >>> import mymodule
  22. The .otherfile import worked
  23. >>> mymodule.run_me()
  24. Hello World?
  25. >>>
  26.  
  27. [username@localhost mymodule]# python3.6
  28. Python 3.6.5 (default, Apr 10 2018, 17:08:37)
  29. [GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
  30. Type "help", "copyright", "credits" or "license" for more information.
  31. >>> import mymodule
  32. Traceback (most recent call last):
  33. File "<stdin>", line 1, in <module>
  34. File "/home/testuser/src/mymodule/mymodule/__init__.py", line 1, in <module>
  35. from .main import run_me
  36. File "/home/testuser/src/mymodule/mymodule/main.py", line 3, in <module>
  37. from otherfile import some_func
  38. ModuleNotFoundError: No module named 'otherfile'
Add Comment
Please, Sign In to add comment