Python3 Mlpy Installation Error - 'Py_Initmodule3' Was Not Declared in This Scope

Python3 mlpy installation error - ‘Py_InitModule3’ was not declared in this scope

error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

mlpy-3.5.0 https://sourceforge.net/projects/mlpy/files/ → Release date = 2012-03-12 → Older gcc, g++ to be used.

sudo apt install g++ g++-5 libgsl-dev
tar xvf mlpy-3.5.0.tar.gz
cd mlpy-3.5.0/

export CC=gcc-5 CXX=g++-5 && python3 setup.py build && sudo python3 setup.py install

No errors.

Import Error: No module called magic yet python-magic is installed

You have installed magic for Python 2.7, but Diffoscope uses Python 3 and explicitly recommends the package python3-magic in the repositories, which can be installed with sudo apt-get install python3-magic. Modules installed for Python 2.7 are not necessarily shared with Python 3, so you may need to install both versions if you need it for 2.7 as well.

On Ubuntu, you can run Python 3 with python3 and access Python 3's pip installation with pip3 to ensure that you are using the correct version.

PyImport_Import fails (returns NULL)

I have resolved this issue by setting PYTHONPATH to pwd. Also module name (without .py) should be set for argv[1].

Thank you!

Cannot find reference 'xxx' in __init__.py

This is a bug in pycharm. PyCharm seems to be expecting the referenced module to be included in an __all__ = [] statement.

For proper coding etiquette, should you include the __all__ statement from your modules? ..this is actually the question we hear young Spock answering while he was being tested, to which he responded: "It is morally praiseworthy but not morally obligatory."

To get around it, you can simply disable that (extremely non-critical) (highly useful) inspection globally, or suppress it for the specific function or statement.

To do so:

  • put the caret over the erroring text ('choice', from your example above)
  • Bring up the intention menu (alt-enter by default, mine is set to alt-backspace)
  • hit the right arrow to open the submenu, and select the relevant action

PyCharm has its share of small bugs like this, but in my opinion its benefits far outweigh its drawbacks. If you'd like to try another good IDE, there's also Spyder/Spyderlib.

I know this is quite a bit after you asked your question, but I hope this helps (you, or someone else).

Edited: Originally, I thought that this was specific to checking __all__, but it looks like it's the more general 'Unresolved References' check, which can be very useful. It's probably best to use statement-level disabling of the feature, either by using the menu as mentioned above, or by specifying # noinspection PyUnresolvedReferences on the line preceding the statement.

ImportError: cannot import name

The problem is that you have a circular import:
in app.py

from mod_login import mod_login

in mod_login.py

from app import app

This is not permitted in Python. See Circular import dependency in Python for more info. In short, the solution are

  • either gather everything in one big file
  • delay one of the import using local import

Automatically Update Python source code (imports)

Behind the scenes, IDEs are no much more than text editors with bunch of windows and attached binaries to make different kind of jobs, like compiling, debugging, tagging code, linting, etc. Eventually one of those libraries can be used to refactor code. One such library is Jedi, but there is one that was specifically made to handle refactoring, which is rope.

pip3 install rope

A CLI solution

You can try using their API, but since you asked for a command line tool and there wasn't one, save the following file anywhere reachable (a known relative folder your user bin, etc) and make it executable chmod +x pyrename.py.

#!/usr/bin/env python3
from rope.base.project import Project
from rope.refactor.rename import Rename
from argparse import ArgumentParser

def renamodule(old, new):
prj.do(Rename(prj, prj.find_module(old)).get_changes(new))

def renamethod(mod, old, new, instance=None):
mod = prj.find_module(mod)
modtxt = mod.read()
pos, inst = -1, 0
while True:
pos = modtxt.find('def '+old+'(', pos+1)
if pos < 0:
if instance is None and prepos > 0:
pos = prepos+4 # instance=None and only one instance found
break
print('found', inst, 'instances of method', old+',', ('tell which to rename by using an extra integer argument in the range 0..' if (instance is None) else 'could not use instance=')+str(inst-1))
pos = -1
break
if (type(instance) is int) and inst == instance:
pos += 4
break # found
if instance is None:
if inst == 0:
prepos = pos
else:
prepos = -1
inst += 1
if pos > 0:
prj.do(Rename(prj, mod, pos).get_changes(new))

argparser = ArgumentParser()
#argparser.add_argument('moduleormethod', choices=['module', 'method'], help='choose between module or method')
subparsers = argparser.add_subparsers()
subparsermod = subparsers.add_parser('module', help='moduledottedpath newname')
subparsermod.add_argument('moduledottedpath', help='old module full dotted path')
subparsermod.add_argument('newname', help='new module name only')
subparsermet = subparsers.add_parser('method', help='moduledottedpath oldname newname')
subparsermet.add_argument('moduledottedpath', help='module full dotted path')
subparsermet.add_argument('oldname', help='old method name')
subparsermet.add_argument('newname', help='new method name')
subparsermet.add_argument('instance', nargs='?', help='instance count')
args = argparser.parse_args()
if 'moduledottedpath' in args:
prj = Project('.')
if 'oldname' not in args:
renamodule(args.moduledottedpath, args.newname)
else:
renamethod(args.moduledottedpath, args.oldname, args.newname)
else:
argparser.error('nothing to do, please choose module or method')

Let's create a test environment with the exact the scenario shown in the question (here assuming a linux user):

cd /some/folder/

ls pyrename.py # we are in the same folder of the script

# creating your test project equal to the question in prj child folder:
mkdir prj; cd prj; cat << EOF >> main.py
#!/usr/bin/env python3
from a.b import foo_method

foo_method()
EOF
mkdir a; touch a/__init__.py; cat << EOF >> a/b.py
def foo_method():
print('yesterday i was foo, tomorrow i will be bar')
EOF
chmod +x main.py

# testing:
./main.py
# yesterday i was foo, tomorrow i will be bar
cat main.py
cat a/b.py

Now using the script for renaming modules and methods:

# be sure that you are in the project root folder

# rename package (here called module)
../pyrename.py module a b
# package folder 'a' renamed to 'b' and also all references

# rename module
../pyrename.py module b.b d
# 'b.b' (previous 'a.b') renamed to 'd' and also all references also
# important - oldname is the full dotted path, new name is name only

# rename method
../pyrename.py method b.d foo_method bar_method
# 'foo_method' in package 'b.d' renamed to 'bar_method' and also all references
# important - if there are more than one occurence of 'def foo_method(' in the file,
# it is necessary to add an extra argument telling which (zero-indexed) instance to use
# you will be warned if multiple instances are found and you don't include this extra argument

# testing again:
./main.py
# yesterday i was foo, tomorrow i will be bar
cat main.py
cat b/d.py

This example did exact what the question did.

Only renaming of modules and methods were implemented because it is the question scope. If you need more, you can increment the script or create a new one from scratch, learning from their documentation and from this script itself. For simplicity we are using current folder as the project folder, but you can add an extra parameter in the script to make it more flexible.

Trying to add CNN to an MLP Siamese

Looks like your reshaping after the convolution is wrong. The output of the convolution layer would be 14x14x32 for a 28x28x1 input passed through conv(stride=1)-maxpool(stride 2). So you need to change the flatten layer to :

  h_out_flat = tf.reshape(out ,[-1,14*14*32])

and also the weights_for_connected_layer appropriately.



Related Topics



Leave a reply



Submit