I Have a Python project which I have built in PyCharm, which works if I run from PyCharm, however now I am ready to run it from command line I get the error.
ImportError: No module named 'integration'
From what I have seen on other similar issues you need to set PYTHONPATH to be the working directory, which I have also done:
[nathan#Linux-Macbook integration]$ export PYTHONPATH=/home/nathan/mobileSubscriberDB/integration/integration
[nathan#Linux-Macbook integration]$ echo $PYTHONPATH
/home/nathan/mobileSubscriberDB/integration/integration
Then when I run the command below (Which I am also within the directory specified on the PYTHONPATH), I also get the error straight after:
[nathan#Linux-Macbook integration]$ /home/nathan/python_msdb/bin/python /home/nathan/mobileSubscriberDB/integration/integration/main.py
Traceback (most recent call last):
File "/home/nathan/mobileSubscriberDB/integration/integration/main.py", line 5, in <module>
from integration import bigchain
ImportError: No module named 'integration'
Below is my project structure:
Below is a Copy of my main.py:
import threading
import sys
import logging
from integration import bigchain
from integration import config
from integration import log
from integration import rabbitMq, file
def do_something_with_exception():
exc_type, exc_value = sys.exc_info()[:2]
print('Handling %s exception with message "%s" in %s' %
(exc_type.__name__, exc_value, threading.current_thread().name))
class ConsumerThread(threading.Thread):
def __init__(self, queue, blockchain_host, blockchain_port, rabbit_props, blockchain_props,*args, **kwargs):
super(ConsumerThread, self).__init__(*args, **kwargs)
logger.debug("Data Type: %s" % queue)
self._queue = queue
logger.debug("Blockchain Host: %s" % blockchain_host)
self._blockchain_host = blockchain_host
logger.debug("Blockchain Port: %s" % blockchain_port)
self._blockchain_port = blockchain_port
self._rabbit_props = rabbit_props
self._blockchain_props = blockchain_props
def run(self):
logger.info("Connecting to BigChain DB for %s Data Type" % self._queue)
bigchaindb = bigchain.BigChain()
bdb = bigchaindb.connect(self._blockchain_host,
self._blockchain_port)
private_key = file.File.read_file(self._blockchain_props['Private Key Path'])
public_key = file.File.read_file(self._blockchain_props['Public Key Path'])
keys = bigchaindb.loadkeys(private_key, public_key)
logger.info('Connecting to RabbitMQ for %s Data Type' % self._queue)
rabbit = rabbitMq.RabbitMq(self._rabbit_props,
self._queue,
bdb,
keys)
channel = rabbit.connect()
rabbit.consume(channel)
if __name__ == "__main__":
log = log.Logger('/etc/msdb/integration/logger.ini')
log.load_config()
logger = logging.getLogger('main')
logger.info("Starting Mobile Subscriber Database Integration...")
threads = []
logger.debug("Created Empty Threads Array")
logger.info("Loading Configuration")
config = config.Config('/etc/msdb/integration/config.ini')
properties = config.load_config()
sections = config.get_sections(properties)
logger.debug("Getting RabbitMQ Properties")
Rabbit_Props = config.loadSection('RabbitMQ', properties)
logger.debug("Getting Blockchain Properties")
Blockchain_Props = config.loadSection('Blockchain', properties)
logger.debug("Loading Data Type Properties")
for section in sections:
props = config.loadSection(section, properties)
if (section != 'RabbitMQ') or (section != 'Blockchain'):
logger.debug("Loading %s Properties" % section)
if props.getboolean('Enabled'):
logger.debug("Section Enabled, Creating Thread")
threads.append(
ConsumerThread(
props['RabbitMQ Queue'],
props['Blockchain Hostname'],
int(props['Blockchain Port']),
Rabbit_Props,
Blockchain_Props
)
)
else:
logger.debug("Section Disabled")
logger.info("Starting Threads")
for thread in threads:
thread.daemon = True
thread.start()
logger.info("Joining Threads")
for thread in threads:
thread.join()
exit(0)
Related
I am using https://github.com/ekonda/kutana.
plugins/test.py:
from kutana import Plugin
import pickle
plugin = Plugin(name="Tictactoe",
description="Tictactoe Game",
invites={},
games=[])
#plugin.on_start()
async def _():
plugin.games = [Game()]
# Backup games when bot is shutting down
#plugin.on_shutdown()
async def _():
try:
with open("games.pickle", "wb") as f:
pickle.dump(plugin.games, f)
except Exception as e:
print(e)
class Game:
def __init__(self):
pass
run.py:
from kutana import Kutana, load_plugins
# Create application
app = Kutana()
# Load and register plugins
app.add_plugins(load_plugins("plugins/"))
if __name__ == "__main__":
# Run application
app.run()
When plugin.games is empty, pickle dumps without errors. But if I put any created object of class Game here, I get an error when trying to backup:
Traceback (most recent call last):
File "C:\Users\root\Desktop\Celestiana\plugins\games\tictactoe.py", line 17, in _
pickle.dump(pl.games, f)
_pickle.PicklingError: Can't pickle <class 'plugins/games\tictactoe.py.Game'>: import of module 'plugins/games\\tictactoe.py' failed
How can I fix it? I try to do something like pickle.dump(plugin.games, f, fix_imports=True) but it does not work.
Fix how you import a Python source file directly.
In load_plugins_from_file, specify the correct name.
def load_plugins_from_file(path, verbose=False):
...
# mod = import_module(path, path) # Change this
name = path.split(".py")[0].replace("/", ".") # to these
mod = import_module(name, path) # two lines
...
In your custom import_module, insert the module into sys.modules as shown in the recipe https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly.
def import_module(name, path):
...
spec = importlib.util.spec_from_file_location(name, os.path.abspath(path))
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module # Add this
spec.loader.exec_module(module)
return module
I have three different files:
A class file lang.py that has the following function
from errors import StatusCodeError
class LangModel:
def __init__(self, localpath2fst, host="127.0.0.1", port="5000", logfile="log"):
...
def state_update(self):
"""
Initialize the language model (on the server side)
"""
try:
r = requests.post(
'http://' +
self.host +
':' +
self.port +
'/init')
except requests.ConnectionError:
raise ConnectionErr(self.host, self.port)
if not r.status_code == requests.codes.ok:
raise StatusCodeError(r.status_code)
An error file errors.py:
class StatusCodeError(Exception):
def __init__(self, status_code):
self.dErrArgs = status_code
Exception.__init__(
self,
"Connection was not established and has status code {0}".format(self.dErrArgs))
and a third file test.py
import unittest
from lang import LangModel
from errors import StatusCodeError
class TestPreLM(unittest.TestCase):
def test_incorrect_input(self):
"""
confirm the process provides
an error given an incorrect
input
"""
abs_path_fst = os.path.abspath("a valid path")
# local fst
localfst = abs_path_fst
# init LMWrapper
lmodel = LangModel(
localfst,
host='127.0.0.1',
port='5000',
logfile="lmwrap.log")
# try to get priors
with self.assertRaises(StatusCodeError):
lmodel.state_update(['3'])
Oddly enough when I have the test code found within the class file, the test succeeds but when it is found in isolation (in test.py) the test fails and I'm unsure why. If you can lighten my way I'll be happy ;)
I have a definition inside of a class which handles an exception in a manner I don't like.
The class itself is inside a module, which itself is called by a module that I import.
the error handling I don't like looks like this:
class BitSharesWebsocket(Events):
#[snip]
def run_forever(self):
""" This method is used to run the websocket app continuously.
It will execute callbacks as defined and try to stay
connected with the provided APIs
"""
cnt = 0
while not self.run_event.is_set():
cnt += 1
self.url = next(self.urls)
log.debug("Trying to connect to node %s" % self.url)
try:
# websocket.enableTrace(True)
self.ws = websocket.WebSocketApp(
self.url,
on_message=self.on_message,
on_error=self.on_error,
on_close=self.on_close,
on_open=self.on_open
)
self.ws.run_forever()
except websocket.WebSocketException as exc:
if (self.num_retries >= 0 and cnt > self.num_retries):
raise NumRetriesReached()
sleeptime = (cnt - 1) * 2 if cnt < 10 else 10
if sleeptime:
log.warning(
"Lost connection to node during wsconnect(): %s (%d/%d) "
% (self.url, cnt, self.num_retries) +
"Retrying in %d seconds" % sleeptime
)
time.sleep(sleeptime)
I wish to preempt the exception here:
except websocket.WebSocketException as exc:
and handle it in my own way, namely to try a new address rather than trying the same address again and again.
I am presented with this exception when calling:
from bitshares.blockchain import Blockchain
from bitshares import BitShares
try:
chain = Blockchain(bitshares_instance=BitShares(n))
except:
print ('hello world')
pass
when n is a bad/unresponsive websocket address
I never get the 'hello world' message because the module handles the exception before I do.
the module is hosted at github here:
https://github.com/xeroc/python-bitshares/blob/9250544ca8eadf66de31c7f38fc37294c11f9548/bitsharesapi/websocket.py
I can do:
from bitsharesapi import websocket as ws
but I am not sure what to do with the module ws now that it is imported to preempt its exception handling, or if this is even the correct way to approach it.
I resolved my issue here:
chain = Blockchain(bitshares_instance=BitShares(n))
I was able to do:
chain = Blockchain(bitshares_instance=BitShares(n,num_retries=0))
I had previously tried this and assumed it wouldn't work:
chain = Blockchain(bitshares_instance=BitShares(n),num_retries=0)
*note parenthesis placement
Could anyone please help me in getting a way to get the source code from Environment or SB Console or Weblogic.
I created the python script whick exports the JAR, but I need the source code. Because if I unjar the jar, I do not get the exact source code, as file names are shorten and some code is added by itself in wsdls, xqueries etc. I don't want that.
Here's my wlst Python/Jython Script:
from java.io import FileInputStream
from java.io import FileOutputStream
from java.util import ArrayList
from java.util import Collections
from com.bea.wli.sb.util import EnvValueTypes
from com.bea.wli.config.env import EnvValueQuery;
from com.bea.wli.config import Ref
from com.bea.wli.config.customization import Customization
from com.bea.wli.config.customization import FindAndReplaceCustomization
import sys
#=======================================================================================
# Utility function to load properties from a config file
#=======================================================================================
def exportAll(exportConfigFile):
def exportAll(exportConfigFile):
try:
print "Loading export config from :", exportConfigFile
exportConfigProp = loadProps(exportConfigFile)
adminUrl = exportConfigProp.get("adminUrl")
exportUser = exportConfigProp.get("exportUser")
exportPasswd = exportConfigProp.get("exportPassword")
exportJar = exportConfigProp.get("exportJar")
customFile = exportConfigProp.get("customizationFile")
passphrase = exportConfigProp.get("passphrase")
project = sys.argv[2]
if project == None :
project = exportConfigProp.get("project")
connectToServer(exportUser, exportPasswd, adminUrl)
ALSBConfigurationMBean = findService("ALSBConfiguration", "com.bea.wli.sb.management.configuration.ALSBConfigurationMBean")
print "ALSBConfiguration MBean found"
print "Input project: ", project
if project == None :
ref = Ref.DOMAIN
collection = Collections.singleton(ref)
if passphrase == None :
print "Export the config"
theBytes = ALSBConfigurationMBean.exportProjects(collection, None)
else :
print "Export and encrypt the config"
theBytes = ALSBConfigurationMBean.export(collection, true, passphrase)
else :
ref = Ref.makeProjectRef(project);
print "Export the project", project
collection = Collections.singleton(ref)
theBytes = ALSBConfigurationMBean.export(collection, false, None)
aFile = File(exportJar)
out = FileOutputStream(aFile)
out.write(theBytes)
out.close()
print "ALSB Configuration file: "+ exportJar + " has been exported"
if customFile != None:
print collection
query = EnvValueQuery(None, Collections.singleton(EnvValueTypes.WORK_MANAGER), collection, false, None, false)
customEnv = FindAndReplaceCustomization('Set the right Work Manager', query, 'Production System Work Manager')
print 'EnvValueCustomization created'
customList = ArrayList()
customList.add(customEnv)
print customList
aFile = File(customFile)
out = FileOutputStream(aFile)
Customization.toXML(customList, out)
out.close()
print "ALSB Dummy Customization file: "+ customFile + " has been created"
except:
raise
#=======================================================================================
# Utility function to load properties from a config file
#=======================================================================================
def loadProps(configPropFile):
propInputStream = FileInputStream(configPropFile)
configProps = Properties()
configProps.load(propInputStream)
return configProps
#=======================================================================================
# Connect to the Admin Server
#=======================================================================================
def connectToServer(username, password, url):
connect(username, password, url)
domainRuntime()
# EXPORT script init
try:
exportAll(sys.argv[1])
except:
print "Unexpected error: ", sys.exc_info()[0]
dumpStack()
raise
Any help would be appreciated.
What you get as a result of the export is the deployed unit. Yes, there is some metadata added/modified as a result of the deployment on the OSB runtime (deployment could also mean creating/editing components directly on the servicebus console).
To get it back as "source code" from the exported jar, you can simply import it back into JDeveloper (12c) or Eclipse with OEPE (11g)
I started learning this asyncore.dispatcher module and when I run the first example program it gives the error as per below:
Python version 2.6
asyncore module is installed and there is also dispatcher class inside it. What may be the problem !
Error:
AttributeError: 'module' object has no attribute 'dispatcher'
Example code:
import asyncore, socket
class HTTPClient(asyncore.dispatcher):
def __init__(self, host, path):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect( (host, 80) )
self.buffer = 'GET %s HTTP/1.0\r\n\r\n' % path
def handle_connect(self):
pass
def handle_close(self):
self.close()
def handle_read(self):
print self.recv(8192)
def writable(self):
return (len(self.buffer) > 0)
def handle_write(self):
sent = self.send(self.buffer)
self.buffer = self.buffer[sent:]
client = HTTPClient('www.python.org', '/')
asyncore.loop()
Your problem is that you named your file asyncore.py. It's shadowing the asyncore.py in the python standard lib so the file is importing itself instead of the real one. You want to rename your copy of the file and delete asyncore.pyc in the same directory if it exists. Then when you run your file, you'll be importing the asyncore.py from the standard library.
When Python runs the line import asyncore, python looks through the directories in sys.path for a file named asyncore.py. The directory of the primary file that's executing is always the first entry in it. So Python finds your file and attempts to import it. As a general rule, you should never give your files the same name as a module from the standard library if you want to use that module.