@support
Yes, pip is way faster. Thanks!
I might have found an even faster solution but I guess I have to wait a few hours to find out if it really works.
Here's what I did:
I created a folder in /root/books called "modules" to install cvxpy there to make it persistent:
!mkdir modules && pip install --target=modules cvxpy
Then if the import fails in the strategy, it creates symbolic links in /usr/local/lib/python3.7/site-packages/ that point to the content of /root/books/modules/
try:
import cvxpy as cp
except ImportError:
import os
source = '/root/book/modules/'
target = '/usr/local/lib/python3.7/site-packages/'
for dirpath, dirnames, filenames in os.walk(source):
source_path = dirpath.replace(source, '')
target_path = os.path.join(target, source_path)
if not os.path.exists(target_path) and not os.path.islink(target_path):
os.symlink(dirpath, target_path)
continue
for file in filenames:
source_file = os.path.join(dirpath, file)
target_file = os.path.join(target, source_path, file)
if not os.path.exists(target_file) and not os.path.islink(target_file):
os.symlink(source_file, target_file)
import cvxpy as cp
Creating the symlinks only takes 0.07 seconds, so fingers crossed 🙂
UPDATE (a few hours later):
It actually worked. When I just reopened the strategy, the environment was newly initialized. First I tried just importing cvxpy and got the ModuleNotFoundError. Then I ran the strategy including the code above: cvxpy was imported correctly and the strategy ran.
I'm not sure if that solution works for every module because I don't know if pip might also write something to other directories than site-packages.
Anyway, I'm happy with this solution.
Regards