Cannot access moonshot strategies from satellite

Hi.

Trying to create satellite script that will, among other things, import a moonshot strategy from codeload and call its methods. The line:

  from moonshot import Moonshot

which is at the beginning of basically every strategy triggers error "No module named ''moonshot''". BTW no logs are created so it took me time to realize where the error is, as there is a name confusion with quantrocket.moonshot and codeload.moonshot.
The same code from Jupyter works.

Tried adding

-e git://github.com/quantrocket-llc/moonshot#egg=moonshot

to quantrocket.satellite.pip.txt - got requirement already satisfied.

The idea is to create a script that will run strategies with little bit more control than just 'quantrocket moonshot trade' command. Is this something not allowed in the quantrocket system?

Thanks!

Try this:

pip install quantrocket-moonshot

Same - requirement already satisfied.
I narrowed it a bit. If I have test.py file in codeload with the code:

import moonshot
from quantrocket.flightlog import FlightlogHandler
print("Test passed")

def test():
    logger = logging.getLogger("test1")
    logger.setLevel(logging.DEBUG)
    handler = FlightlogHandler()
    logger.addHandler(handler)
    logger.info("Test passed")

If I do:

quantrocket satellite exec 'python /codeload/test.py'

it works. But if I call:

quantrocket satellite exec codeload.test.test

I get "No module named 'moonshot'" error. So I will probably stick to calling python directly in this case.
Thanks!

Actually running it with 'python' won't help.
My problem was my script was in the /codeload/ root and lovely python import system. Just as experiment navigate to codeload, start terminal and run python from terminal, now enter:

from moonshot import Moonshot

It gives you error because it tries to import from codeload.moonshot. What happened to "global modules first"?

I've got it working, still the question - is it possible to stop quantrocket from adding init.py files into every folder?

Thanks!

Hmm, I'm not able to reproduce this. Relative imports are disabled via /opt/conda/lib/python3.6/site-packages/sitecustomize.py.

    # Disable relative imports (users must import from codeload)
    if "" in sys.path:
        sys.path.remove("")

This means import moonshot won't look for codeload.moonshot even if you're in the codeload directory. This sitecustomize.py file gets installed on both jupyter and satellite. This has been in place since version 1.5.

In any case you can look at that file and install your own version if needed.

This is it! When I create python console from Jupyter, "" is not in sys.path, but when I call python from bash Terminal, "" is in sys.path for both my old installation and the new one (version 1.9.0). But I can simply add these lines to my scripts now. Thanks!

Hi, I’m having the same issue as OP but when working in Jupyter Lab.

Running:

From moonshot import Moonshot

Triggers the error “No module named ‘moonshot’”.

I tried pip install moonshot in the terminal but this yields “Requirement already satisfied”.

So my guess is the problem seems to be that relative imports are not disabled via sitecustomize.py as you point out, but I am not sure how to access this file to edit (I am new to docker and don’t know how to navigate files in a container).

Appreciate your help!

Instead of editing sitecustomize.py, it might be easier to try adding the above code to disable relative imports at the top of your file before importing moonshot.

Thanks, so after digging a bit more, “” is not in sys.path in Jupyter, but importing moonshot still results in “No module named ‘moonshot’”.

Running:

import sys
print(sys.path)

Shows the following list:

['/opt/conda/envs/zipline/lib/python36.zip', '/opt/conda/envs/zipline/lib/python3.6', '/opt/conda/envs/zipline/lib/python3.6/lib-dynload', '/opt/conda/envs/zipline/lib/python3.6/site-packages', '/opt/conda/envs/zipline/lib/python3.6/site-packages/IPython/extensions', '/root/.ipython']

Any suggestion of what might be happening here? Maybe the directory containing moonshot is not in sys.path?

Ok my mistake! I was working from a zipline environment notebook (python 3.6), which doesn't have moonshot. I was able to import moonshot just fine when working from the standard kernel (python 3.8).
Thanks