Last active
October 25, 2023 10:13
-
-
Save sepastian/9b1fa65049f717ba6ffe6bae8cafd56d to your computer and use it in GitHub Desktop.
(DEPRECATED) Install any pip package in Coderpad
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# DEPRECATED: | |
# In addition to running Python code, the coderpad console allows | |
# to pip install packages as well: | |
# | |
# >>> pip install sortedcontainers | |
# | |
# Using the code below is not required. | |
# It will be kept here for reference, as it shows how to access | |
# the Coderpad environment. | |
# Based off https://gist.github.com/SegFaultAX/2c010063939a4d0712e6 | |
def install_dep(package): | |
""" | |
Install a package in Coderpad using pip. | |
Copy/paste this method into your Python coderpad; | |
run it, passing in the package to install as a string. | |
package ... the package to install | |
Returns the exit code of `pip install ...`. | |
""" | |
if package == None: | |
raise Exception("no package given") | |
import os | |
import sys | |
from pathlib import Path | |
import subprocess | |
# Determine, where to install packages. | |
u = Path(os.environ.get("HOME")) | |
v = f"{sys.version_info.major}.{sys.version_info.minor}" | |
site_packages = u.joinpath('.local', 'lib', f'python{v}', 'site-package') | |
sys.path.append(site_packages) | |
# Invoke pip to install selected package. | |
return subprocess.call(["pip", "install", "--user", package]) | |
# Let's try this, install the sortedcontainers package. | |
package = "sortedcontainers" | |
if install_dep(package) != 0: | |
raise Exception(f"Installation of package {package} failed!") | |
import sortedcontainers as sc | |
sl = sc.SortedList() | |
sl.add(10) | |
sl.add(5) | |
sl.add(15) | |
print(sl) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment