Import from python library on bluemix

☆樱花仙子☆ 提交于 2019-12-13 07:21:35

问题


I need to use the following imports in a Jupyter notebook on IBM Bluemix

import swiftclient
from keystoneclient import client

If I were on my local workstation, I could use pip to install the libraries, but how do I do it on Bluemix? Another post suggests adding a requirements.txt file and a Procfile file into the root directory of my Python application, but I don't see how to do this with a notebook. Any suggestions?

Here is the code:

#!pip install --upgrade pip
!pip install python-swiftclient
!pip install python-keystoneclient

import swiftclient
from keystoneclient import client

# Object Store credentials (generated by Insert to code)
credentials = {
  'auth_url':'****',
  'project':'****',
  'project_id':'****',
  'region':'dallas',
  'user_id':'****',
  'domain_id':'****',
  'domain_name':'****',
  'username':'****',
  'password':"""****""",
  'filename':'Warehousing-data.json',
  'container':'notebooks',
  'tenantId':'****'
}

# Establish Connection to Bluemix Object Store
connection = swiftclient.Connection(
    key=credentials[password],
    authurl=credentials[auth_url],
    auth_version='3',
    os_options={"project_id": credentials[projectId],
                "user_id": credentials[userId],
                "region_name": credentials[region]})

# The data files should now be accessible through calls of the form
# connection.get_object(credentials[container], fileName)[1]

# See https://developer.ibm.com/recipes/tutorials/using-ibm-object-storage-in-bluemix-with-python/
# and https://github.com/saviosaldanha/IBM_Object_Store_Python_Example/blob/master/storage_recipe_example.py

Here are the error messages:

You are using pip version 7.1.0, however version 8.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting python-swiftclient
  Using cached python_swiftclient-3.0.0-py2.py3-none-any.whl
Requirement already satisfied (use --upgrade to upgrade): futures>=2.1.3 in /gpfs/global_fs01/sym_shared/YPProdSpark/user/sbf1-4c17d3407da8d0-a7ea98a5cc6d/.local/lib/python2.7/site-packages (from python-swiftclient)
Requirement already satisfied (use --upgrade to upgrade): requests>=1.1 in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/requests-2.7.0-py2.7.egg (from python-swiftclient)
Requirement already satisfied (use --upgrade to upgrade): six>=1.5.2 in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/six-1.9.0-py2.7.egg (from python-swiftclient)
Installing collected packages: python-swiftclient
Exception:
Traceback (most recent call last):
  File "/usr/local/src/bluemix_ipythonspark_16/notebook/lib/python2.7/site-packages/pip-7.1.0-py2.7.egg/pip/basecommand.py", line 223, in main
    status = self.run(options, args)
  File "/usr/local/src/bluemix_ipythonspark_16/notebook/lib/python2.7/site-packages/pip-7.1.0-py2.7.egg/pip/commands/install.py", line 299, in run
    root=options.root_path,
  File "/usr/local/src/bluemix_ipythonspark_16/notebook/lib/python2.7/site-packages/pip-7.1.0-py2.7.egg/pip/req/req_set.py", line 646, in install
    **kwargs
  File "/usr/local/src/bluemix_ipythonspark_16/notebook/lib/python2.7/site-packages/pip-7.1.0-py2.7.egg/pip/req/req_install.py", line 813, in install
    self.move_wheel_files(self.source_dir, root=root)
  File "/usr/local/src/bluemix_ipythonspark_16/notebook/lib/python2.7/site-packages/pip-7.1.0-py2.7.egg/pip/req/req_install.py", line 1008, in move_wheel_files
    isolated=self.isolated,
  File "/usr/local/src/bluemix_ipythonspark_16/notebook/lib/python2.7/site-packages/pip-7.1.0-py2.7.egg/pip/wheel.py", line 339, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/usr/local/src/bluemix_ipythonspark_16/notebook/lib/python2.7/site-packages/pip-7.1.0-py2.7.egg/pip/wheel.py", line 310, in clobber
    ensure_dir(destdir)
  File "/usr/local/src/bluemix_ipythonspark_16/notebook/lib/python2.7/site-packages/pip-7.1.0-py2.7.egg/pip/utils/__init__.py", line 70, in ensure_dir
    os.makedirs(path)
  File "/usr/local/src/bluemix_ipythonspark_16/notebook/lib/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/usr/local/src/bluemix_ipythonspark_16/notebook/lib/python2.7/site-packages/swiftclient'

You are using pip version 7.1.0, however version 8.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Requirement already satisfied (use --upgrade to upgrade): python-keystoneclient in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/python_keystoneclient-1.6.0-py2.7.egg
Requirement already satisfied (use --upgrade to upgrade): pbr<2.0,>=0.11 in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/pbr-0.11.0-py2.7.egg (from python-keystoneclient)
Requirement already satisfied (use --upgrade to upgrade): argparse in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/argparse-1.3.0-py2.7.egg (from python-keystoneclient)
Requirement already satisfied (use --upgrade to upgrade): Babel>=1.3 in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/Babel-1.3-py2.7.egg (from python-keystoneclient)
Requirement already satisfied (use --upgrade to upgrade): iso8601>=0.1.9 in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/iso8601-0.1.10-py2.7.egg (from python-keystoneclient)
Requirement already satisfied (use --upgrade to upgrade): netaddr>=0.7.12 in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/netaddr-0.7.15-py2.7.egg (from python-keystoneclient)
Requirement already satisfied (use --upgrade to upgrade): oslo.config>=1.11.0 in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/oslo.config-1.15.0-py2.7.egg (from python-keystoneclient)
Requirement already satisfied (use --upgrade to upgrade): oslo.i18n>=1.5.0 in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/oslo.i18n-1.5.0-py2.7.egg (from python-keystoneclient)
Requirement already satisfied (use --upgrade to upgrade): oslo.serialization>=1.4.0 in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/oslo.serialization-1.4.0-py2.7.egg (from python-keystoneclient)
Requirement already satisfied (use --upgrade to upgrade): oslo.utils>=1.4.0 in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/oslo.utils-1.9.0-py2.7.egg (from python-keystoneclient)
Requirement already satisfied (use --upgrade to upgrade): PrettyTable<0.8,>=0.7 in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/prettytable-0.7-py2.7.egg (from python-keystoneclient)
Requirement already satisfied (use --upgrade to upgrade): requests>=2.5.2 in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/requests-2.7.0-py2.7.egg (from python-keystoneclient)
Requirement already satisfied (use --upgrade to upgrade): six>=1.9.0 in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/six-1.9.0-py2.7.egg (from python-keystoneclient)
Requirement already satisfied (use --upgrade to upgrade): stevedore>=1.3.0 in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/stevedore-1.6.0-py2.7.egg (from python-keystoneclient)
Requirement already satisfied (use --upgrade to upgrade): pip in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/pip-7.1.0-py2.7.egg (from pbr<2.0,>=0.11->python-keystoneclient)
Requirement already satisfied (use --upgrade to upgrade): pytz>=0a in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/pytz-2015.4-py2.7.egg (from Babel>=1.3->python-keystoneclient)
Requirement already satisfied (use --upgrade to upgrade): msgpack-python>=0.4.0 in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/msgpack_python-0.4.5-py2.7-linux-x86_64.egg (from oslo.serialization>=1.4.0->python-keystoneclient)
Requirement already satisfied (use --upgrade to upgrade): monotonic>=0.1 in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/monotonic-0.2-py2.7.egg (from oslo.utils>=1.4.0->python-keystoneclient)
Requirement already satisfied (use --upgrade to upgrade): netifaces>=0.10.4 in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/netifaces-0.10.4-py2.7-linux-x86_64.egg (from oslo.utils>=1.4.0->python-keystoneclient)
Requirement already satisfied (use --upgrade to upgrade): debtcollector>=0.3.0 in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/debtcollector-0.5.0-py2.7.egg (from oslo.utils>=1.4.0->python-keystoneclient)
Requirement already satisfied (use --upgrade to upgrade): wrapt>=1.7.0 in /usr/local/src/bluemix_ipythonspark_16/notebook-2.7/lib/python2.7/site-packages/wrapt-1.10.5-py2.7-linux-x86_64.egg (from debtcollector>=0.3.0->oslo.utils>=1.4.0->python-keystoneclient)

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-12-7298587a7c89> in <module>()
      3 get_ipython().system(u'pip install python-keystoneclient')
      4 
----> 5 import swiftclient
      6 from keystoneclient import client
      7 

ImportError: No module named swiftclient

I don't know why it complains about the pip version. Doesn't Bluemix control that? And when I used !pip install --upgrade pip it complained about that too. I've been trying to make this work for over a month! Very frustrating.


回答1:


You need to use the --user flag, because as user you do not root access to the system.

Try the following code:

!pip install --user python-swiftclient
!pip install --user python-keystoneclient

However, I can see from you error messages that you service instance is quite old. It would be better, if you switch to the new offering "IBM Data Science Experience": http://datascience.ibm.com/



来源:https://stackoverflow.com/questions/37599212/import-from-python-library-on-bluemix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!