stefan-jansen / zipline-reloaded

Zipline, a Pythonic Algorithmic Trading Library
https://zipline.ml4trading.io
Apache License 2.0
1.18k stars 216 forks source link

ModuleNotFoundError: No module named 'zipline.api'; 'zipline' is not a package #251

Closed soleilinvesting closed 5 months ago

soleilinvesting commented 5 months ago

Dear Zipline Maintainers,

Before I tell you about my issue, let me describe my environment:

Environment

* Operating System: (Mac Sonoma 14.3 M1 chip) * Python Version: `$ python 3.11.7` * Python Bitness: `$ python -c 'import math, sys;print(int(math.log(sys.maxsize + 1, 2) + 1))'` * How did you install Zipline: (`pip`, `conda`, or `other (please explain)`) *I installed zipline in the PyCharm IDE and within the terminal I am using the Pycharm IDE 2023.3.4 (professional edition) Python packages: `$ pip freeze` or `$ conda list` adjustText==1.1.1 alembic==1.13.1 altair==5.3.0 appdirs==1.4.4 arch==6.3.0 astropy==6.0.0 astropy-iers-data==0.2024.3.4.0.30.17 asttokens==2.4.1 attrs==23.2.0 bcolz-zipline==1.2.9 beautifulsoup4==4.12.3 blosc2==2.5.1 Bottleneck==1.3.8 certifi==2024.2.2 charset-normalizer==3.3.2 clarabel==0.7.1 click==8.1.7 cloudpickle==3.0.0 colorlog==6.8.2 comm==0.2.1 contourpy==1.2.0 cvxpy==1.4.2 cycler==0.12.1 dask==2024.2.1 dateparser==1.2.0 decorator==5.1.1 dill==0.3.8 dmba==0.2.4 duckdb==0.10.0 ecos==2.0.13 empyrical-reloaded==0.5.10 exchange_calendars==4.5.4 executing==2.0.1 fastjsonschema==2.19.1 fonttools==4.49.0 frozendict==2.4.0 fsspec==2024.2.0 future==1.0.0 graphviz==0.20.3 h11==0.14.0 h5py==3.11.0 hmmlearn==0.3.2 html5lib==1.1 humanize==4.9.0 hyperopt==0.2.7 idna==3.6 imageio==2.34.0 importlib-metadata==7.0.1 intervaltree==3.1.0 ipython==8.22.2 ipywidgets==8.1.2 iso3166==2.1.1 iso4217==1.11.20220401 jedi==0.19.1 Jinja2==3.1.4 joblib==1.3.2 jsonlines==4.0.0 jsonschema==4.22.0 jsonschema-specifications==2023.12.1 jupyter_core==5.7.2 jupyterlab_widgets==3.0.10 kaleido==0.2.1 kiwisolver==1.4.5 korean-lunar-calendar==0.3.1 llvmlite==0.42.0 locket==1.0.0 lru-dict==1.3.0 lxml==5.1.0 Mako==1.3.2 MarkupSafe==2.1.5 matplotlib==3.8.3 matplotlib-inline==0.1.6 msgpack==1.0.8 multipledispatch==1.0.0 multiprocess==0.70.16 multitasking==0.0.11 mypy-extensions==1.0.0 nbformat==5.10.4 ndindex==1.8 networkx==3.2.1 numba==0.59.0 numexpr==2.9.0 numpy==1.26.4 optuna==3.5.0 osqp==0.6.5 outcome==1.3.0.post0 packaging==23.2 pandas==2.2.1 parso==0.8.3 partd==1.4.1 pathos==0.3.2 patsy==0.5.6 peewee==3.17.1 pexpect==4.9.0 pillow==10.2.0 platformdirs==4.2.1 plotly==5.19.0 polars==0.20.17 pox==0.3.4 ppft==1.7.6.8 prince==0.13.0 prompt-toolkit==3.0.43 ptyprocess==0.7.0 pure-eval==0.2.2 py-cpuinfo==9.0.0 py4j==0.10.9.7 pyarrow==15.0.0 pybind11==2.11.1 pydash==7.0.7 pyerfa==2.0.1.1 Pygments==2.17.2 pykalman==0.9.7 pyluach==2.2.0 pyparsing==3.1.1 pyportfolioopt==1.5.5 PySocks==1.7.1 python-dateutil==2.9.0.post0 python-interface==1.6.1 pytz==2024.1 PyYAML==6.0.1 qdldl==0.1.7.post0 QuantStats==0.0.62 referencing==0.35.1 regex==2023.12.25 requests==2.31.0 Riskfolio-Lib==5.0.1 rpds-py==0.18.1 schedule==1.2.1 scikit-learn==1.4.1.post1 scipy==1.11.4 scs==3.2.4.post1 seaborn==0.13.2 sec-api==1.0.17 selenium==4.21.0 six==1.16.0 smtpapi==0.4.12 sniffio==1.3.1 sortedcontainers==2.4.0 soupsieve==2.5 SQLAlchemy==2.0.28 stack-data==0.6.3 statsmodels==0.14.1 TA-Lib==0.4.28 tables==3.9.2 tabulate==0.9.0 tenacity==8.2.3 threadpoolctl==3.3.0 toolz==0.12.1 tqdm==4.66.2 traitlets==5.14.1 trio==0.25.1 trio-websocket==0.11.1 typing_extensions==4.10.0 tzdata==2024.1 tzlocal==5.2 urllib3==2.2.1 vectorbtpro @ git+https://github.com/polakowo/vectorbt.pro.git@7ec7b3262e9b77882f88550c0501358362eb614b wcwidth==0.2.13 webencodings==0.5.1 websocket-client==1.7.0 widgetsnbextension==4.0.10 wsproto==1.2.0 XlsxWriter==3.2.0 yfinance==0.2.37 zipline-reloaded==3.0.4 zipp==3.17.0

Now that you know a little about me, let me tell you about the issue I am having:

Description of Issue

I have tried to run several different zipline programs and recienve the same error. I did this also on my Windows machine with the same error.

Here is how you can reproduce this issue on your machine:

Reproduction Steps

I run the zipline tutorial: ` import zipline from zipline import order_target, record, symbol

def initialize(context): context.i = 0 context.asset = symbol('AAPL')

def handle_data(context, data):

Skip first 300 days to get full windows

context.i += 1
if context.i < 300:
    return

# Compute averages
# data.history() has to be called with the same params
# from above and returns a pandas dataframe.
short_mavg = data.history(context.asset, 'price', bar_count=100, frequency="1d").mean()
long_mavg = data.history(context.asset, 'price', bar_count=300, frequency="1d").mean()

# Trading logic
if short_mavg > long_mavg:
    # order_target orders as many shares as needed to
    # achieve the desired number of shares.
    order_target(context.asset, 100)
elif short_mavg < long_mavg:
    order_target(context.asset, 0)

# Save values for later inspection
record(AAPL=data.current(context.asset, 'price'),
       short_mavg=short_mavg,
       long_mavg=long_mavg)

` And I get the below error: Traceback (most recent call last): File "/Users/ryanalex/PycharmProjects/scientificProject/.venv/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3577, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 1, in runfile('/Users/ryanalex/Dropbox/Python/Maschine_Learning_Jansen/zipline_tutorial.py', wdir='/Users/ryanalex/Dropbox/Python/Maschine_Learning_Jansen') File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_bundle/pydev_umd.py", line 197, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/Users/ryanalex/Dropbox/Python/Maschine_Learning_Jansen/zipline_tutorial.py", line 2, in import zipline File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import module = self._system_import(name, *args, *kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/Users/ryanalex/Dropbox/Python/Maschine_Learning_Jansen/zipline.py", line 12, in from zipline.api import ( File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import module = self._system_import(name, args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ModuleNotFoundError: No module named 'zipline.api'; 'zipline' is not a package ...

What steps have you taken to resolve this already?

I have tried

...

Anything else?

This may be a PyCharm issue. It feels like ...

Sincerely, Real name is Ryan and I am reading your book on Machine Learning, it is a great book. Thank you for all you do within the community.

soleilinvesting commented 5 months ago

I resolved the issue.