Hi, I'm trying to import a python class that I wrote into my main.py file. However I can't seem to figure out the import structure. I'm able to get it to work by adding a library, but I don't really want to create a new library, and libraries aren't included in the code section under backtests which I like to reference at times.
If someone has an example of doing regular python imports it would be much appreciated!
Adam W
You don't need to import anything if it's defined globally, e.g.
main.py
If `MyLibrary` is saved in some other file like `MyLibrary.py`, you can just import the class with `from MyLibrary import MyLibrary`.
Wesley Smith
Hmmmm my classes aren't defined globally though, they are defined in a different file. For example:
If this is my project structure, how can I import classes from test.py into main.py?
The library imports are working fine for me, but it's not exactly what I want. Thanks!
Fred Painchaud
Hi Wesley,
Basically just like Adam was writing / like any other import in Python.
Say you have the class MyClass defined in test.py, you write:
from test import MyClass
Fred
Wesley Smith
Hi Fred, when I try that, I get the following error:
Any thoughts on why that might be?
Adam W
Might be easier if you attach a simplified project. Is your project like this?
Fred Painchaud
Hello,
The error “cannot import name 'MyClass'” usually means that you have a circular import dependency to MyClass.
So one file X needs MyClass so it imports it but the file in which MyClass is needs something in X so it imports it. That simple example does not account for indirect circular references but you can also have that and it would also throw an error.
Circular import dependencies are very easy to solve in Python because you can defer the import statement (which in Python is not a static statement for the pre-compiler like in many other languages but an executable statement, just like all other executable statements).
But to help you defer the right statement and at the right place, yes, we would need more info on your project. All we need is the import portion of all your files, which it seems, you only have two, main and test. So your circular dependency must be that main imports something in test but test also imports something in main.
Fred
Wesley Smith
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!