robertmartin8 / PyPortfolioOpt

Financial portfolio optimisation in python, including classical efficient frontier, Black-Litterman, Hierarchical Risk Parity
https://pyportfolioopt.readthedocs.io/
MIT License
4.26k stars 929 forks source link

Update Dockerfile #498

Closed 88d52bdba0366127fffca9dfa93895 closed 1 year ago

yosukesan commented 1 year ago

@xtuanta Have you tested ?

Guest OS trying to use pip==21.0.1 and poetry==1.1.4". Then failed on my env. See Err Log. pip and poetry version are hard coded on docker/Dockerfile. I believe this is not good manner.

Since pyproject.toml and poetry.lock are copied to guest env and used. I think it is safe to just remove hard coded version. After I did so, docker image was successfully generated. See Normally terminated case.

Since @xtuanta already sent a patch, could I ask additional fix ? I'll manually test on your branch. Better to add docker build and test workflow as well, but probably better to make another issue for it.

I forgot to mention. buster is debian's old stable which has EOL next summer. Better to switch to current stable version.

Normally terminated case: Changes from robertmartin8's original

diff --git a/docker/Dockerfile b/docker/Dockerfile
index 1a339b9..57b5601 100644
--- a/docker/Dockerfile
+++ b/docker/Dockerfile
@@ -1,16 +1,16 @@
-FROM python:3.8-buster
+FROM python:3.9-bullseye

 WORKDIR pypfopt
 COPY pyproject.toml poetry.lock ./

-RUN pip install --upgrade pip==21.0.1 && \
-    pip install "poetry==1.1.4" && \
+RUN pip install --upgrade pip && \
+    pip install "poetry" && \
     pip install yfinance && \
     poetry install -E optionals --no-root 

 COPY . .

-RUN cd cookbooks
+RUN cd cookbook

Err Log: Err termination by using pip==21.0.1 and poetry==1.1.4

yosuke@localhost:~/Data/finance/py_portfolio_opt/PyPortfolioOpt$ docker build -f docker/Dockerfile . -t pyopttest
Sending build context to Docker daemon  18.03MB
Step 1/6 : FROM python:3.8-buster
 ---> 1c8fafb52ada
Step 2/6 : WORKDIR pypfopt
 ---> Running in 1b77a3d2b1cf
Removing intermediate container 1b77a3d2b1cf
 ---> e5976a8103ac
Step 3/6 : COPY pyproject.toml poetry.lock ./
 ---> eaed4c6a64bb
Step 4/6 : RUN pip install --upgrade pip==21.0.1 &&     pip install "poetry==1.1.4" &&     pip install yfinance &&     poetry install -E optionals --no-root
 ---> Running in 44edffe5ec46
Collecting pip==21.0.1
  Downloading pip-21.0.1-py3-none-any.whl (1.5 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.5/1.5 MB 6.2 MB/s eta 0:00:00
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 22.0.4
    Uninstalling pip-22.0.4:
      Successfully uninstalled pip-22.0.4
Successfully installed pip-21.0.1
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
Collecting poetry==1.1.4
  Downloading poetry-1.1.4-py2.py3-none-any.whl (171 kB)
Collecting keyring<22.0.0,>=21.2.0
  Downloading keyring-21.8.0-py3-none-any.whl (32 kB)
Collecting shellingham<2.0,>=1.1
  Downloading shellingham-1.5.0-py2.py3-none-any.whl (9.3 kB)
Collecting packaging<21.0,>=20.4
  Downloading packaging-20.9-py2.py3-none-any.whl (40 kB)
Collecting pkginfo<2.0,>=1.4
  Downloading pkginfo-1.9.2-py3-none-any.whl (26 kB)
Collecting tomlkit<1.0.0,>=0.7.0
  Downloading tomlkit-0.11.6-py3-none-any.whl (35 kB)
Collecting virtualenv<21.0.0,>=20.0.26
  Downloading virtualenv-20.17.0-py3-none-any.whl (8.8 MB)
Collecting requests-toolbelt<0.10.0,>=0.9.1
  Downloading requests_toolbelt-0.9.1-py2.py3-none-any.whl (54 kB)
Collecting cachecontrol[filecache]<0.13.0,>=0.12.4
  Downloading CacheControl-0.12.11-py2.py3-none-any.whl (21 kB)
Collecting clikit<0.7.0,>=0.6.2
  Downloading clikit-0.6.2-py2.py3-none-any.whl (91 kB)
Collecting pexpect<5.0.0,>=4.7.0
  Downloading pexpect-4.8.0-py2.py3-none-any.whl (59 kB)
Collecting poetry-core<2.0.0,>=1.0.0
  Downloading poetry_core-1.4.0-py3-none-any.whl (546 kB)
Collecting requests<3.0,>=2.18
  Downloading requests-2.28.1-py3-none-any.whl (62 kB)
Collecting crashtest<0.4.0,>=0.3.0
  Downloading crashtest-0.3.1-py3-none-any.whl (7.0 kB)
Collecting cachy<0.4.0,>=0.3.0
  Downloading cachy-0.3.0-py2.py3-none-any.whl (20 kB)
Collecting cleo<0.9.0,>=0.8.1
  Downloading cleo-0.8.1-py2.py3-none-any.whl (21 kB)
Collecting html5lib<2.0,>=1.0
  Downloading html5lib-1.1-py2.py3-none-any.whl (112 kB)
Collecting msgpack>=0.5.2
  Downloading msgpack-1.0.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (322 kB)
Collecting lockfile>=0.9
  Downloading lockfile-0.12.2-py2.py3-none-any.whl (13 kB)
Collecting pylev<2.0,>=1.3
  Downloading pylev-1.4.0-py2.py3-none-any.whl (6.1 kB)
Collecting pastel<0.3.0,>=0.2.0
  Downloading pastel-0.2.1-py2.py3-none-any.whl (6.0 kB)
Collecting webencodings
  Downloading webencodings-0.5.1-py2.py3-none-any.whl (11 kB)
Collecting six>=1.9
  Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Collecting jeepney>=0.4.2
  Downloading jeepney-0.8.0-py3-none-any.whl (48 kB)
Collecting SecretStorage>=3.2
  Downloading SecretStorage-3.3.3-py3-none-any.whl (15 kB)
Collecting pyparsing>=2.0.2
  Downloading pyparsing-3.0.9-py3-none-any.whl (98 kB)
Collecting ptyprocess>=0.5
  Downloading ptyprocess-0.7.0-py2.py3-none-any.whl (13 kB)
Collecting urllib3<1.27,>=1.21.1
  Downloading urllib3-1.26.13-py2.py3-none-any.whl (140 kB)
Collecting certifi>=2017.4.17
  Downloading certifi-2022.9.24-py3-none-any.whl (161 kB)
Collecting charset-normalizer<3,>=2
  Downloading charset_normalizer-2.1.1-py3-none-any.whl (39 kB)
Collecting idna<4,>=2.5
  Downloading idna-3.4-py3-none-any.whl (61 kB)
Collecting cryptography>=2.0
  Downloading cryptography-38.0.4-cp36-abi3-manylinux_2_28_x86_64.whl (4.2 MB)
Collecting cffi>=1.12
  Downloading cffi-1.15.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (442 kB)
Collecting pycparser
  Downloading pycparser-2.21-py2.py3-none-any.whl (118 kB)
Collecting distlib<1,>=0.3.6
  Downloading distlib-0.3.6-py2.py3-none-any.whl (468 kB)
Collecting platformdirs<3,>=2.4
  Downloading platformdirs-2.5.4-py3-none-any.whl (14 kB)
Collecting filelock<4,>=3.4.1
  Downloading filelock-3.8.0-py3-none-any.whl (10 kB)
Installing collected packages: pycparser, urllib3, idna, charset-normalizer, cffi, certifi, requests, pylev, pastel, msgpack, jeepney, cryptography, crashtest, webencodings, six, SecretStorage, pyparsing, ptyprocess, platformdirs, lockfile, filelock, distlib, clikit, cachecontrol, virtualenv, tomlkit, shellingham, requests-toolbelt, poetry-core, pkginfo, pexpect, packaging, keyring, html5lib, cleo, cachy, poetry
Successfully installed SecretStorage-3.3.3 cachecontrol-0.12.11 cachy-0.3.0 certifi-2022.9.24 cffi-1.15.1 charset-normalizer-2.1.1 cleo-0.8.1 clikit-0.6.2 crashtest-0.3.1 cryptography-38.0.4 distlib-0.3.6 filelock-3.8.0 html5lib-1.1 idna-3.4 jeepney-0.8.0 keyring-21.8.0 lockfile-0.12.2 msgpack-1.0.4 packaging-20.9 pastel-0.2.1 pexpect-4.8.0 pkginfo-1.9.2 platformdirs-2.5.4 poetry-1.1.4 poetry-core-1.4.0 ptyprocess-0.7.0 pycparser-2.21 pylev-1.4.0 pyparsing-3.0.9 requests-2.28.1 requests-toolbelt-0.9.1 shellingham-1.5.0 six-1.16.0 tomlkit-0.11.6 urllib3-1.26.13 virtualenv-20.17.0 webencodings-0.5.1
WARNING: You are using pip version 21.0.1; however, version 22.3.1 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
Collecting yfinance
  Downloading yfinance-0.1.87-py2.py3-none-any.whl (29 kB)
Collecting appdirs>=1.4.4
  Downloading appdirs-1.4.4-py2.py3-none-any.whl (9.6 kB)
Collecting pandas>=0.24.0
  Downloading pandas-1.5.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (12.2 MB)
Requirement already satisfied: requests>=2.26 in /usr/local/lib/python3.8/site-packages (from yfinance) (2.28.1)
Collecting numpy>=1.15
  Downloading numpy-1.23.5-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.1 MB)
Collecting multitasking>=0.0.7
  Downloading multitasking-0.0.11-py3-none-any.whl (8.5 kB)
Collecting lxml>=4.5.1
  Downloading lxml-4.9.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl (6.9 MB)
Collecting python-dateutil>=2.8.1
  Downloading python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
Collecting pytz>=2020.1
  Downloading pytz-2022.6-py2.py3-none-any.whl (498 kB)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.8/site-packages (from python-dateutil>=2.8.1->pandas>=0.24.0->yfinance) (1.16.0)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.8/site-packages (from requests>=2.26->yfinance) (3.4)
Requirement already satisfied: charset-normalizer<3,>=2 in /usr/local/lib/python3.8/site-packages (from requests>=2.26->yfinance) (2.1.1)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.8/site-packages (from requests>=2.26->yfinance) (2022.9.24)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.8/site-packages (from requests>=2.26->yfinance) (1.26.13)
Installing collected packages: pytz, python-dateutil, numpy, pandas, multitasking, lxml, appdirs, yfinance
Successfully installed appdirs-1.4.4 lxml-4.9.1 multitasking-0.0.11 numpy-1.23.5 pandas-1.5.2 python-dateutil-2.8.2 pytz-2022.6 yfinance-0.1.87
WARNING: You are using pip version 21.0.1; however, version 22.3.1 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.
Traceback (most recent call last):
  File "/usr/local/bin/poetry", line 5, in <module>
    from poetry.console import main
  File "/usr/local/lib/python3.8/site-packages/poetry/console/__init__.py", line 1, in <module>
    from .application import Application
  File "/usr/local/lib/python3.8/site-packages/poetry/console/application.py", line 7, in <module>
    from .commands.about import AboutCommand
  File "/usr/local/lib/python3.8/site-packages/poetry/console/commands/__init__.py", line 2, in <module>
    from .add import AddCommand
  File "/usr/local/lib/python3.8/site-packages/poetry/console/commands/add.py", line 8, in <module>
    from .init import InitCommand
  File "/usr/local/lib/python3.8/site-packages/poetry/console/commands/init.py", line 16, in <module>
    from poetry.core.pyproject import PyProjectException
ImportError: cannot import name 'PyProjectException' from 'poetry.core.pyproject' (/usr/local/lib/python3.8/site-packages/poetry/core/pyproject/__init__.py)
The command '/bin/sh -c pip install --upgrade pip==21.0.1 &&     pip install "poetry==1.1.4" &&     pip install yfinance &&     poetry install -E optionals --no-root' returned a non-zero code: 1
88d52bdba0366127fffca9dfa93895 commented 1 year ago

@yosukesan Good point, absolutely agree with you.

Your changes are good, could you make a PR, maybe in this patch. Because the dockerfile is not a main file for this project, it focuses more on the cookbook, therefore some hardcoded libraries are acceptable in this case. Hardcoded versions are not.

For an official dockerfile to build this library, we should have a minimal working dockerfile, maybe in the root folder, not in the docker subfolder. That's quite easy, just simply remove the hardcoded libraries and clean the dockerfile. How do you think about this @robertmartin8

robertmartin8 commented 1 year ago

I never use docker, so I don't have opinion on this. Happy to defer to yours – let me know when this is good to be merged!

yosukesan commented 1 year ago

I'm going to send additional amendment to @88d52bdba0366127fffca9dfa93895's fix since current patch terminated with err when I tested.

Your changes are good, could you make a PR, maybe in this patch

I'm unfamiliar to send additional patch over a patch. Should I send to https://github.com/88d52bdba0366127fffca9dfa93895/PyPortfolioOpt/tree/patch-1 ?

88d52bdba0366127fffca9dfa93895 commented 1 year ago

@yosukesan Yes, sure. Please let me known when it is ready for review.

yosukesan commented 1 year ago

I just send a patch. Manually tested on my machine and all passed. See below for the detail. https://github.com/88d52bdba0366127fffca9dfa93895/PyPortfolioOpt/pull/1

robertmartin8 commented 1 year ago

@88d52bdba0366127fffca9dfa93895 if all looks good, I can merge. Let me know

88d52bdba0366127fffca9dfa93895 commented 1 year ago

LGTM. Many thanks @yosukesan @robertmartin8

yosukesan commented 1 year ago

Thanks @robertmartin8, but only @88d52bdba0366127fffca9dfa93895's patch was merged. I think @88d52bdba0366127fffca9dfa93895 didn't resend pull request after my patch was merged.

Could I make additional issue and send patch for it ? Probably easier to chase the change log later.

robertmartin8 commented 1 year ago

Sure

88d52bdba0366127fffca9dfa93895 commented 1 year ago

Sorry guys, I forgot to merge the patch. Thanks @yosukesan for your finding.

yosukesan commented 1 year ago

Sorry guys, I forgot to merge the patch. Thanks @yosukesan for your finding.

No worries. I didn't know git worked this way either.