Name conversion is not defined - python

I have an error when running the following in python
Runtime error
Traceback (most recent call last):
File "", line 6, in
File "C:\Python27\ArcGIS10.3\Lib\rpy2\robjects__init__.py", line 55, in
conversion.ri2py = default_ri2py
NameError: name 'conversion' is not defined
this is my script init.py
import rpy2.rinterface as rinterface
import rpy2.rlike.container as rlc
import rpy2.robjects.conversion
conversion.ri2py = default_ri2py

Variable simply not defined. Use the snippet below to define it.
import rpy2.robjects as robjects
robjects.conversion.ri2py = default_ri2py
http://rpy.sourceforge.net/rpy2/doc-2.1/html/robjects_convert.html

Related

Wierd attribute error with importing into directories

I have a directory tree as follows:
main.py
dir1
sub1.py
sub2.py
In main.py:
import dir1.sub1
In dir1/sub1.py:
def f1() -> None:
print("f1")
import dir1.sub2
dir1.sub2.f2()
In dir1/sub2.py:
import dir1.sub1
def f2() -> None:
dir1.sub1.f1()
print("f2")
When I run main.py, I get the following error message:
Traceback (most recent call last):
File "...\main.py", line 1, in <module>
import dir1.sub1
File "...\dir1\sub1.py", line 7, in <module>
dir1.sub2.f2()
File "...\dir1\sub2.py", line 5, in f2
dir1.sub1.f1()
AttributeError: module 'dir1' has no attribute 'sub1'. Did you mean: 'sub2'?
(Where the ... at the beginning of the file path is my working directory.)
If I change main.py to
import dir1.sub2
I get a slightly different error message:
Traceback (most recent call last):
File "...\main.py", line 1, in <module>
import dir1.sub2
File "...\dir1\sub2.py", line 1, in <module>
import dir1.sub1
File "...\dir1\sub1.py", line 7, in <module>
dir1.sub2.f2()
AttributeError: module 'dir1' has no attribute 'sub2'
If I move sub1.py and sub2.py to the same directory as main.py and re‐direct imports as necessary, I get the expected output of
f1
f2
Why does this happen, and how can I make it not happen?
You need to use absolute import because Python 3 only supports that. In Python 2 your method will work. So for example if you have import dir1.sub2 change it to from dir1 import sub2. See here.
Note: I've tested it with your setup and it works.

importing other file and using functions from main

Note: I have reduced my problem so the code is only a few lines (compared to 600)
I have a problem: from main.py I want to import file slave.py. slave.py references a function from main.py, and of course I get a NameError: name 'funcFromMain' is not defined
Here is my code for main.py:
import slave
def funcFromMain():
return 6
print(slave.funcFromSlave())
And here is my code for slave.py:
def funcFromSlave():
one = funcFromMain() # <- this doesn't work
two = 2
return (one + two)
I am getting exact error: (note that both files are in exactly the same directory)
Traceback (most recent call last):
File "C:\Users\PrinceOfCreation\Documents\test\main.py", line 6, in <module>
print(slave.funcFromSlave())
File "C:\Users\PrinceOfCreation\Documents\test\slave.py", line 2, in funcFromSlave
one = funcFromMain()
NameError: name 'funcFromMain' is not defined
I tried adding import main at the top of slave.py, and got the following error:
Traceback (most recent call last):
File "C:\Users\PrinceOfCreation\Documents\test\main.py", line 1, in <module>
import slave
File "C:\Users\PrinceOfCreation\Documents\test\slave.py", line 1, in <module>
import main
File "C:\Users\PrinceOfCreation\Documents\test\main.py", line 6, in <module>
print(slave.funcFromSlave())
AttributeError: module 'slave' has no attribute 'funcFromSlave'
With from slave import funcFromSlave instead at the top of main:
Traceback (most recent call last):
File "C:\Users\PrinceOfCreation\Documents\test\main.py", line 6, in <module>
print(funcFromSlave())
File "C:\Users\PrinceOfCreation\Documents\test\slave.py", line 2, in funcFromSlave
one = funcFromMain()
NameError: name 'funcFromMain' is not defined
First you can't import a python module like this :
import slave.py
It must be
from slave import funcFromSlave # to get the funcFromSlave function from slave script
And you need to make sure that the slave.py is in the same directory of main.py or
you need to precise the subdirectory where slave.py exists
And for the later error, its best if you avoid circular imports, cause it will create problems, best to do is to send the value of funcFromMain() to funcFromSlave
main.py :
from slave import funcFromSlave
def funcFromMain():
return 6
print(funcFromSlave(funcFromMain()))
slave.py :
def funcFromSlave(funcFromMain):
one = funcFromMain
two = 2
return (one + two)
output when running main.py :
8

gremlin python retrieve ids and labels (valueMap(True))

python g.V('test_red1').valueMap().toList()
works fine but when I pass true to request ids and labels I get this error. Anything I am missing?
g.V('test_red1').valueMap(True).toList()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ec2-user/environment/merchantGraph/gremlin_python/process/traversal.py", line 52, in toList
return list(iter(self))
File "/home/ec2-user/environment/merchantGraph/gremlin_python/process/traversal.py", line 43, in __next__
...
Am I missing something. I am using AWS Neptune ...
I am adding extra import statements
and traceback
import time
import requests
import json
from gremlin_python import statics
from gremlin_python.structure.graph import Graph
from gremlin_python.process.graph_traversal import __
from gremlin_python.process.strategies import *
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
import boto3
from os import environ
graph = Graph()
g = graph.traversal().withRemote(DriverRemoteConnection(environ['gremlinNeptuneConnection'],'g'))
# this works
g.V('test_red1').valueMap().toList()
# this fails
g.V('test_red1').valueMap(True).toList()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ec2-user/environment/merchantGraph/gremlin_python/process/traversal.py", line 52, in toList
return list(iter(self))
....
File "/home/ec2-user/environment/merchantGraph/gremlin_python/structure/io/graphsonV3d0.py", line 455, in objectify
new_dict[reader.toObject(l[x])] = reader.toObject(l[x + 1])
TypeError: unhashable type: 'dict'
My guess is that you are running into trouble with this recently reported bug with valueMap(true):
https://issues.apache.org/jira/browse/TINKERPOP-1860
it is patched and will be fixed with release of 3.3.2. Until then you will have to work around the issue as there really is no fix short of reverting to GraphSON 2.0 (which comes with it's own set of downsides). One workaround would be to project() your results:
gremlin> g.V().project('props','id','label').
......1> by(valueMap()).
......2> by(id).
......3> by(label)
==>[props:[name:[marko],age:[29]],id:1,label:person]
==>[props:[name:[vadas],age:[27]],id:2,label:person]
==>[props:[name:[lop],lang:[java]],id:3,label:software]
==>[props:[name:[josh],age:[32]],id:4,label:person]
==>[props:[name:[ripple],lang:[java]],id:5,label:software]
==>[props:[name:[peter],age:[35]],id:6,label:person]

Define method from external module (Python)

I'm trying to define a method (let's call it hello) in main.py's Things class, from define.py.
The code I currently have is raising AttributeError: module 'runme' has no attribute 'promptMe' (full traceback below).
Here's main.py's code:
import define
class Things:
def doWhatever():
print("whatever")
Here's define.py's code:
import main
def hello():
print("Hello!")
main.Things.hello = hello()
I've tried other solutions such as def main.Things.hello: hello() and def main.Things.hello: print("Hello!") but none work.
Here's the traceback when running define.py:
Traceback (most recent call last):
File "define.py", line 5, in <module>
import main
File "/path/to/main.py", line 9, in <module>
import define
File "/path/to/define.py", line 10, in <module>
main.Things.hello = hello
AttributeError: module 'main' has no attribute 'Things'
All help would be greatly appreciated. Thanks!

Import error python

In Python, when I use this import statement breze.learn.mlp import iter_minibatches, am getting the following errors.
Here iter_minibatches is a function defined in mlp.py.
Traceback (most recent call last):
File "/home/vinod/PycharmProjects/MLPonTheano/MLPbreze.py", line 15, in <module>
from breze.learn.mlp import Mlp, FastDropoutNetwork
File "/home/vinod/breze/breze/learn/mlp.py", line 22, in <module>
from breze.learn.base import SupervisedModel
File "/home/vinod/breze/breze/learn/base.py", line 21, in <module>
from breze.learn.mlp import iter_minibatches
ImportError: cannot import name iter_minibatches
You have a circular import; mlp imports base imports mlp:
# executing mlp.py
File "/home/vinod/breze/breze/learn/mlp.py", line 22, in <module>
from breze.learn.base import SupervisedModel
# executing base.py
File "/home/vinod/breze/breze/learn/base.py", line 21, in <module>
# this tries to import from mlp again, but mlp isn't done yet
from breze.learn.mlp import iter_minibatches
Any line after the from breze.learn.base import SupervisedModel will not yet have been executed so importing any object defined by those lines will fail.
Avoid circular imports, or if you must have them, delay importing in one of the modules to make sure the objects you need in the other are defined.

Categories

Resources