## Packages
import sys
import os
import glob
import json
import matplotlib.pyplot as plt
import watson_developer_cloud
## Cloud service credential connection
discovery_creds = helper.fetch_credentials('discovery')
discovery = watson_developer_cloud.DiscoveryV1(
version='2018-08-01',
url=discovery_creds['url'],
iam_apikey=discovery_creds['apikey'])
## Environment initialization
env, env_id = helper.fetch_object(
discovery, "environment", "Compugin",
create=True, create_args=dict(
description="Compugin 1.0 -- Question/Answering"
))
# Lists existing configurations for the service instance and store default configuration id
configurations = discovery.list_configurations(environment_id=env_id).get_result()
cfg_id = configurations['configurations'][0]['configuration_id']
print(json.dumps(configurations, indent=2))
# List default configuration details
config = discovery.get_configuration(environment_id=env_id, configuration_id=cfg_id).get_result()
print(json.dumps(config, indent=2))
# Test configuration on some sample text
data_dir = "data"
filename = os.path.join(data_dir, "sample.html")
with open(filename, "r") as f:
res = discovery.test_configuration_in_environment(environment_id=env_id, configuration_id=cfg_id, file=f).get_result()
print(json.dumps(res, indent=2))
When trying to run the above python code, I receive this error:
Traceback (most recent call last):
File "compugin.py", line 7, in
import watson_developer_cloud
ImportError: No module named watson_developer_cloud
I have installed the watson_developer_cloud package using pip, not sure what I'm doing wrong.
There are two worlds when we install packages using pip - Global site-packages and virtualenv packages
Creating Virtual Environments Python “Virtual Environments” allow Python packages to be installed in an isolated location for a
particular application, rather than being installed globally.
Imagine you have an application that needs version 1 of LibFoo, but
another application requires version 2. How can you use both these
applications? If you install everything into
/usr/lib/python3.6/site-packages (or whatever your platform’s standard
location is), it’s easy to end up in a situation where you
unintentionally upgrade an application that shouldn’t be upgraded.
Or more generally, what if you want to install an application and
leave it be? If an application works, any change in its libraries or
the versions of those libraries can break the application.
Also, what if you can’t install packages into the global site-packages
directory? For instance, on a shared host.
In all these cases, virtual environments can help you. They have their
own installation directories and they don’t share libraries with other
virtual environments.
Currently, there are two common tools for creating Python virtual
environments:
venv is available by default in Python 3.3 and later, and installs pip
and setuptools into created virtual environments in Python 3.4 and
later. virtualenv needs to be installed separately, but supports
Python 2.7+ and Python 3.3+, and pip, setuptools and wheel are always
installed into created virtual environments by default (regardless of
Python version).
Read installing packages
To under the differences between the global site-packages and virtualenv packages, refer pip installing in global site-packages instead of virtualenv
Check if you have not installed yet then run this to install
pip install watson_developer_cloud
Related
When running following code, user without error reports normal termination.
python -c "import paramiko"
User with error reports termination (note, no exception) with the following:
/usr/local/lib/python2.7/site-packages/cryptography/hazmat/backends/__init__.py:7: UserWarning: Module _hashlib was already imported from /usr/local/lib/python2.7/lib-dynload/_hashlib.so, but /usr/local/lib/python2.7/site-packages/hashlib-20081119-py2.7-linux-i686.egg is being added to sys.path
import pkg_resources
/usr/local/lib/python2.7/site-packages/cryptography/hazmat/backends/__init__.py:7: UserWarning: Module hashlib was already imported from /usr/local/lib/python2.7/hashlib.py, but /usr/local/lib/python2.7/site-packages/hashlib-20081119-py2.7-linux-i686.egg is being added to sys.path
import pkg_resources
Both users have the same PYTHONPATH, PATH and LD_LIBRARY_PATH. There are no virtual environments on this machine. All .pyc files have been removed on computer and error persists.
Python Version: 2.7.11
OS: CentOS 32 bit el6
pip freeze reports hashlib==20081119 and cryptography==1.7.2 and paramiko==2.1.1
Does anyone have any insight into this problem? I do not understand why one user can perform this task without error, while the other cannot.
Solved it by reinstalling paramiko with pip, ignoring caches and site-packages
I know this is not an actual info-sec question, but I am having problems with getting the smbrealyx.py module to work. For some reason I get the following error when I try to execute the aforementioned python program.
Traceback (most recent call last):
File "smbrelayx.py", line 43, in <module>
from impacket import smbserver, smb, ntlm, dcerpc, version
File "/usr/lib/python2.7/dist-packages/impacket/smbserver.py", line 18, in <module>
from impacket import smbserver, version
ImportError: cannot import name smbserver
I am not familiar with python programming and I was hoping someone could help me fix this issue.
Looks like you have an old impacket version installed and you are using a newer version of smbrelayx.py.
First of it'd be great to know what version you have. You can easily do that by typing inside a Python interpreter the following:
from impacket import version
print version.BANNER
Assuming you have an old version, first of all it'd be great to remove the existing version. Depending on your Unix distro it might be just as easy as to remove the python-impacket package, or you can manually remove the library files by getting to know where those files are located:
import impacket
print impacket.__file__
That will give you the path where the library is installed. I'd suggest to remove the entire directory.
Now that your system is clean, you have two options:
Install a stable version: Grab the latest stable version from here. Uncompress it in a temp directory and then run:
python setup.py install
That will install the libraries and example scripts (e.g. smbrelayx.py)
Install the development version: You will need to git clone the development version first by running:
git clone https://github.com/CoreSecurity/impacket
Once the repo is cloned, inside the impacket directory type:
python setup.py install
That will install the libraries and example scripts (e.g. smbrelayx.py)
I'm working through a flask tutorial and am trying to run a script that creates a database instead of doing it through the command line. It uses the SQLAlchemy-migrate package, but when I try to run the script, it gives an ImportError.
This is the terminal output:
Sean:app seanpatterson$ python ./db_create.py
Traceback (most recent call last):
File "./db_create.py", line 2, in <module>
from migrate.versioning import api
ImportError: No module named migrate.versioning
This is the db_create.py script:
#!flask/bin/python
from migrate.versioning import api
from config import SQLALCHEMY_DATABASE_URI
from config import SQLALCHEMY_MIGRATE_REPO
from app import db
import os.path
db.create_all()
if not os.path.exists(SQLALCHEMY_MIGRATE_REPO):
api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository')
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
else:
api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO))
This is the config file it references:
#!/usr/bin/env python
import os
basedir = os.path.abspath(os.path.dirname(__file__))
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'app.db')
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')
This application is being run with a virtual environment. This are the module that relates to it that I have installed in the environment:
sqlalchemy_migrate-0.7.2-py2.7.egg-info
Any help appreciated
pip install sqlalchemy==0.7.9
and
pip install sqlalchemy-migrate==0.7.2
and
optionally this flask-whooshalchemy==0.55a should solve the problem
ImportError: No module named migrate.versioning probably means the module is not installed. Make sure it has been installed in the correct virtual environment, it is activated (you ran the activate script in that environment) and the selected Python binary is actually making use of that environment (i.e. you are using Python2 and not Python3).
As said by #BoppreH earlier
ImportError: No module named migrate.versioning
means that the module named 'migrate' is not installed in your virtual environment or your system. First make sure that you are using the proper environment and that it is activated using the activate script.
I had the same problem and had the correct environment set up. But still the error was not solved.
What worked for me was installing the sqlalchemy-migrate package from pip. After activating my environment, I ran the following code to install it :
pip install sqlalchemy-migrate
flask/bin/pip install flask-sqlalchemy without defining the version worked fine for me.
run :
easy_install Flask-SQLAlchemy
to install Flask-SQLAlchemy
sudo pip install flask-migrate
to install flask-migrate
I think this error might pop up for several obscure reasons, I would like to add another which I experienced:
I had the same exact error while having sqlalchemy-migrate correctly installed, and guess what, it didn't work just because I had named the migration script file as migrate.py, this created some conflict with the migrate package.
In fact PyCharm warned me with this message:
"Import resolves to its containing file... This inspection detects names that should resolve but don't."
I renamed the migration script as db_migrate.py and everything started working fine.
I could understand what was the issue cause I had another project with an identical set-up but with migrate-sqlalchemy working perfectly and the only difference was indeed that file name...
Hope this might help someone one day...
I had the same problem - "No module named migrate.versioning", and everything is much easier than we are talking about, you need to perform the commands "run"
file: db_create.py or file: db_migrate.py if you using PyCharm (not from the terminal). And you will have the expected output: "New migration saved as D:...there is my path...\microblog\db_repositort/versions/001_migration.py
Current database version: 1"
As part of my build system, I am using a modified version of a Python package (cogapp). I don't want to install the package because:
I've modified the package and don't want to worry about collision with unmodified versions which may already be installed.
It's nicer if the users of the build system don't need to install extra packages.
However, I'm having problems with using the package if it's not installed. If it is installed, I can run:
python -m cogapp <additional args>
and everything runs as intended.
The package has a __main__.py script:
import sys
from cogapp import Cog
sys.exit(Cog().main(sys.argv))
I tried running this directly, e.g.:
python -m <path>/__main__ <additional_args>
But I get the error:
...
/__main__.py", line 3, in <module>
from cogapp import Cog
ImportError: No module named cogapp
This is probably related to the error I get if I run __init__.py:
from .cogapp import *
The error is:
from .cogapp import *
ValueError: Attempted relative import in non-package
How can I run the package as a package?
EDIT:
I found a fix by removing all the relative imports from cogapp, and removing the -m, i.e. not running as a module. In this instance it's not too bad because it's a small package with only a single directory. However I'm interested in how this should be done in future. There's lots of stuff written around this subject, but no clear answers!
Here's the solution I've come to.
Disclaimer: you are actually installing the package but to a different path than the standard one.
$ mkdir newhome
$ python setup.py install --home=./newhome
$ PYTHONPATH=$PWD/newhome/lib/python <COMMAND_NEEDING_THAT_PACKAGE>
I am working on a computer cluster, which has NumPy 1.4.1 installed in the usual folder (/usr/lib64/....). As I want to use NumPy 1.7.0, I have installed it /.../myPath, and added export PYTHONPATH=/.../myPath to my .bashrc, such that using import numpy will automatically load NumPy 1.7.0. This works fine, except for a peculiarity when using parallel python. To load the correct NumPy module in each process, I modify sys.path, as those processes seem to ignore the $PYTHONPATH variable:
import pp
import numpy
def try2():
sys.path.insert(0,'/.../myPath')
import numpy
a=numpy.random.rand(4,4)
return numpy.__version__
print numpy.__version__
job_server = pp.Server(2, ppservers=() )
jobs=[job_server.submit(try2,(),(),("sys",)),job_server.submit(try2,(),(),("sys",))]
for job in jobs:
print job()
The output is as desired:
1.7.0
1.7.0
1.7.0
However, when I call it with an ndarray argument like this
import pp
import numpy
def try2(a):
sys.path.insert(0,'/.../myPath')
import numpy
return numpy.__version__
print numpy.__version__
a=numpy.random.rand(4,4)
job_server = pp.Server(2, ppservers=() )
jobs=[job_server.submit(try2,(a,),(),("sys",)),job_server.submit(try2,(a,),(),("sys",))]
for job in jobs:
print job()
the output changes to
1.7.0
1.4.1
1.4.1
My interpretation: the subprocess receives a numpy.ndarray argument as soon as it's called and therefore searches a module named numpy before I get a chance to modify sys.path. Any ideas on how to fix this?
Given your particular requirement of differentiation from user and sytem-wide modules given a certain package, I would recommend looking at setting up virtualenv to run all your code in an isolated environment (to quote the link):
sudo pip install virtualenv
(or, sudo easy_install virtualenv if you don’t use pip)
(or, easy_install --install-dir ~/site-packages/ virtualenv on a shared host)
mkdir ~/virtualenvs (a directory for your isolated environments)
virtualenv ~/virtualenvs/mysite.com --no-site-packages
(--no-site-packages isolates your environment from the main site-packages directory)
cd ~/virtualenvs/mysite.com/bin
source activate (activates your new environment)
This would help save the need for path injection.