41881

Python: SystemError Parent module not loaded, cannot perform relative import

Question:

I know there are already a lot of questions similar to this one but unfortunately I haven't found out how to apply to my problem.

The structure of my project is as follows:

Project |___ maincode |___ __init__.py |___ losses.py |___ bin |___ __init__.py |___ train.py

and then in train.py I have the following code:

# Allow relative imports when being executed as script. if __name__ == "__main__" and __package__ is None: __package__ = "maincode.bin" sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..')) from .. import losses def main(args=None): #some code here if __name__ == '__main__': main()

Now if I execute the script train.py from the project main directory Project/, i.e. running the following in the terminal:

maincode/bin/train.py

then I obtain the following error:

<blockquote>

SystemError: Parent module 'maincode.bin' not loaded, cannot perform relative import

</blockquote>

I tried both with Python 2.7.6 and Python 3.5.2 and obtained the same error.

Could you please help? Thank you in advance!

Answer1:

If you just want to run that module and not the whole Package. Do python -m maincode.bin.train which tells python to run it as a module.

If you want to run the whole package then change directory into the Project: cd /path/to/project then run: python maincode and if your code is set up correctly it should run train.py.

Otherwise, move the script outside of the Package and run it on it's own.

More on this and understanding Packages can be found here: <a href="https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time" rel="nofollow">Relative imports for the billionth time</a>

Answer2:

Files in the same directory can only use the "import module" and cannot use the "from" keyword, for unknown reasons

Recommend

  • Idiomatic iterator + mutation on a struct in Rust?
  • Joining two result sets into one
  • Angular mergeMap Types
  • Use Chrome to visit github, can not load css and js. but IE is OK
  • instanceof yields inconsistent results for detecting interfaces?
  • Why does the Try-Catch block affect a variable in an enclosing scope?
  • VsCode Extension: Rename Symbols
  • N+1 in rails relations with active records?
  • Improving Cython Lapack performance with internal array definitions?
  • spark streaming write data to Hbase with python blocked on saveAsNewAPIHadoopDataset
  • Distributed Cache feature in YARN
  • Drag file into Access, how to check the file details?
  • STM32 Rotary Encoder config on TIM4
  • Is it possible to use pseudo-elements to make containing elements wrap around an absolutely-position
  • Passing Control Id from Linkbutton in a gridview
  • RSRuntimeException: Loading of ScriptC script failed (build tools 19.0.2)
  • flexbox sticky footer
  • List (and connect to) Bluetooth devices from within iOS App
  • How to position a Widget at the bottom of a SingleChildScrollView?
  • Bazel failed to include a external static library .a
  • Use PHP to Replace HTML with HTML
  • How to get the Owner of the ContextMenu (from Silverlight 4 toolkit)?
  • Implementation of timeout in LDAP
  • Windows biometric framework sample umdf driver: This device cannot start. (Code 10)
  • Javascript inside HTML import not affecting imported HTML
  • C# code can't “see” the methods in my C++ dll
  • Facebook Error (#200) The user hasn't authorized the application to perform this action (PHP)
  • What are advantages/disadvantages of using Selenium for Java vs .NET applications?
  • Copy and paste data from multiple workbooks to a worksheet in another Workbook
  • Request Access Token in Postman for Azure Function App protected by Azure AD B2C
  • 'Edit' function for forum posts and such
  • Multiplying polynomials/simplifying like terms
  • Create an average of multiple excel chart without the data source
  • C++ STL stack pop operation giving segmentation fault
  • JQuery: Infinite input select
  • Codeigniniter insert data through models and controller
  • Sign a Pdf using custom digital signature in Java