i am trying to convert my python code to dll, in the code below under ffi.embedding_init_code i can import packages which i have installed with pip or conda like cv2, numpy, pil etc but i have created python file my_tools.py this is giving error while accessing the dll. "ModuleNotfoundError: no module named 'my_tools' "
import re
import cffi
ffi = cffi.FFI()
with open('plugin.h') as f:
include = f.read()
ffi.embedding_api(include)
ffi.set_source("my_plugin",
re.sub(r'^extern(?=\s)', 'CFFI_DLLEXPORT', include, flags=re.M))
ffi.embedding_init_code("""
from my_plugin import ffi, lib
import keras_ocr
import my_tools # as m_tools
import logging
import sys
import cv2
import numpy as np
from PIL import Image
import io
import base64
#ffi.def_extern()
def hello(out_result):
out_result=ffi.string(out_result)
print("hello python="+str(out_result))
return 0
""")
ffi.cdef("""
char *strdup(const char *);
""")
ffi.compile(target="plugin-1.5.*", verbose=True)
below is my plugin.h
extern int hello(char* out_result);
how can import my own created file here.
There is no one-size-fits-all answer, but a quick way to get started is to add this as the first line in
embedding_init_code:
import sys; sys.path.insert(0, "/path/containing/the/python/files")
Related
I am trying to load FlaUI libraries using pythonnet. The code is able to load the FlaUI.UIA3.dll. However, importing FlaUI.UIA3 namespace fails.
Here is my code,
import clr
import sys
dll_path = 'C:\\Users\\amit_tendulkar\\.nuget\\packages\\flaui.core\\3.2.0\\lib\\net45'
dll_path2 = 'C:\\Users\\amit_tendulkar\\.nuget\\packages\\flaui.uia3\\3.2.0\\lib\\net45'
sys.path.append(dll_path)
clr.AddReference('FlaUI.Core')
sys.path.append(dll_path2)
clr.AddReference('FlaUI.UIA3')
from FlaUI.Core import Application
from FlaUI.Core.Conditions import ConditionFactory
from FlaUI.Core.Tools import Retry
from FlaUI.UIA3 import UIA3Automation
from FlaUI.UIA3 import UIA3PropertyLibrary
The error I am getting is below (using command python sample.py),
Traceback (most recent call last):
File ".\ToadApp.py", line 12, in <module>
from FlaUI.UIA3 import UIA3Automation
ModuleNotFoundError: No module named 'FlaUI.UIA3'; 'FlaUI' is not a package
If I don't include the FlaUI.UIA3 library then I am able to Launch the application with Application.Launch('software.exe').
Here is the content of my directory containing FlaUI3.UIA3.dll,
C:\Users\amit_tendulkar>dir C:\Users\amit_tendulkar\.nuget\packages\flaui.uia3\3.2.0\lib\net45
Volume in drive C has no label.
Volume Serial Number is 8692-D75E
Directory of C:\Users\amit_tendulkar\.nuget\packages\flaui.uia3\3.2.0\lib\net45
25-01-2022 22:28 <DIR> .
25-01-2022 22:28 <DIR> ..
17-07-2020 02:05 105,472 FlaUI.UIA3.dll
17-07-2020 02:05 28,095 FlaUI.UIA3.xml
2 File(s) 133,567 bytes
Dotnet version (using Windows 10),
C:\Users\amit_tendulkar>dotnet --version
6.0.101
Looks like FlaUI.UIA3.dll has dependency on Interop.UIAutomationClient.dll.
Updating the code to the below solved my issue.
import clr
import sys
flaui_core_path = 'C:\\Users\\amit_tendulkar\\.nuget\\packages\\flaui.core\\3.2.0\\lib\\net45'
flaui_uia3_path = 'C:\\Users\\amit_tendulkar\\.nuget\\packages\\flaui.uia3\\3.2.0\\lib\\net45'
interop_uiautomation_path = 'C:\\Users\\amit_tendulkar\\.nuget\\packages\\interop.uiautomationclient\\10.18362.0\\lib\\net45'
sys.path.append(flaui_core_path)
clr.AddReference('FlaUI.Core')
sys.path.append(interop_uiautomation_path)
clr.AddReference('Interop.UIAutomationClient')
sys.path.append(flaui_uia3_path)
clr.AddReference('FlaUI.UIA3')
from FlaUI.Core import Application
from FlaUI.Core.Conditions import ConditionFactory
from FlaUI.Core.Tools import Retry
from FlaUI.UIA3 import UIA3Automation
from FlaUI.UIA3 import UIA3PropertyLibrary
e.g.
import os as my_os
import my_os.path
ModuleNotFoundError: No module named 'my_os'
but the following script is ok
import os
import os.path
You can't do that in Python.
import statements are importing from Python file names.
You aren't renaming the file of os to my_os, therefore this wouldn't work.
As mentioned in the documentation:
The import statement combines two operations; it searches for the named module, then it binds the results of that search to a name in the local scope.
Tried the similar way as os.py did:
import json as my_json
from my_json.decoder import * # ModuleNotFoundError: No module named 'my_json'
import sys
import json.decoder as my_decoder
sys.modules['my_json.decoder'] = my_decoder
from my_json.decoder import * # it's ok now
I am working with Anaconda-spyder, my scripts are working fine in spyder also I can run it from CMD
but I can not run my scripts with my Nodejs app
Nodejs
const express = require('express');
const router = express.Router();
const {PythonShell} = require("python-shell");
router.get('/hi', (req, res, next)=> {
console.log("here");
PythonShell.run('pyt//home.py', {
args: ['infoo']
}, function (err, results) {
console.log("here2");
if(results)
{ console.log(results);}
if(err)
{ console.log(err);}
});
});
module.exports = router;
In python code, used libraries
import os
import sys
import tweepy,codecs
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import nltk
from textblob import Word
import six
import numpy as np
import pandas as pd
from os import path
from PIL import Image
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
from nltk.corpus import stopwords
from matplotlib.backends.backend_pdf import PdfPages
import time
And getting the argv parameter in home.py with
if __name__ == "__main__":
twt = sys.argv[1]
print("hi python")
When I run server, get the route with postman. console says
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node ./bin/www`
here
here2
{ Error: ModuleNotFoundError: No module named 'pandas'
at PythonShell.parseError (C:\Users\username\Desktop\NodejsBitirme\node_modules\python-shell\index.js:258:21)
at terminateIfNeeded (C:\Users\username\Desktop\NodejsBitirme\node_modules\python-shell\index.js:141:32)
at ChildProcess.<anonymous> (C:\Users\username\Desktop\NodejsBitirme\node_modules\python-shell\index.js:133:13)
at ChildProcess.emit (events.js:198:13)
at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)
----- Python Traceback -----
File "pyt\home.py", line 9, in <module>
Seems like installed libs problem. It work from cmd.
In the same folder
python home.py "hello"
It works
input code:
import os
import logging
import logging.handlers
import random
import numpy as np
import skvideo.io
import cv2
import matplotlib.pyplot as plt
import utils
Some codes here ... ... ... then:
if __name__ == "__main__":
log = utils.init_logging()
if not os.path.exists(IMAGE_DIR):
log.debug("Creating image directory `%s`...", IMAGE_DIR)
os.makedirs(IMAGE_DIR)
main()
Output:
AttributeError: module 'utils' has no attribute 'init_logging'
I have tried to rename the file into utils.py. Nothing
I assume you're making a tutorial of Making Road Traffic Counting App based on Computer Vision and OpenCV. You just need to download from github the file named "utils.py" that the author made.
I was having a play around with Python 2.7 and everybody knows that at the start of every program, you always have to import modules. For example:
import random
import time
for x in range(1, 300):
print random.randint(1,100)
time.sleep(1)
print "Done!"
Anyway, I was thinking, why do I have to import all my modules manually? Why doesn't Python just import them all like this.
Sure, I can understand why it does not import like this:
from random import randint
from time import *
for x in range(1, 300):
print randint(1,100)
sleep(1)
print "Done!"
As some function names may clash. But, if you have to define where the function is at the start, so for example random. in random.randint(1,100).
Now modern computers are so powerful, it seems logical to import every module automatically instead of wasting lines of code, and time by having to find which module you need then importing it manually when it can easily be automated.
So, why does python not import every module at startup automatically?
EDIT:
I have made a new version of a little program that imports every module that I can find by running:
import sys
sys.builtin_module_names
Here are the results:
x = int(1000000)
def test():
global x
x -= 1
print "Iterations Left: ", x
import __builtin__
import __main__
import _ast
import _bisect
import _codecs
import _codecs_cn
import _codecs_hk
import _codecs_iso2022
import _codecs_jp
import _codecs_kr
import _codecs_tw
import _collections
import _csv
import _functools
import _heapq
import _hotshot
import _io
import _json
import _locale
import _lsprof
import _md5
import _multibytecodec
import _random
import _sha
import _sha256
import _sha512
import _sre
import _struct
import _subprocess
import _symtable
import _warnings
import _weakref
import _winreg
import array
import audioop
import binascii
import cPickle
import cStringIO
import cmath
import datetime
import errno
import exceptions
import future_builtins
import gc
import imageop
import imp
import itertools
import marshal
import math
import mmap
import msvcrt
import nt
import operator
import parser
import signal
import strop
import sys
import thread
import time
import xxsubtype
import zipimport
import zlib
def start():
from timeit import Timer
t = Timer("test()", "from __main__ import test")
print t.timeit()
start()
Because you don't need all of it. There is no point in loading every library if you don't need them.
EDIT:
I copied my libs folder to a test directory and made it into a package by adding an __init__.py file to it. In this file I added:
import os
import glob
__all__ = [ os.path.basename(f)[:-3] for f in glob.glob(os.path.dirname(__file__)+"/*.py")]
I created a test script that contains:
from Lib import *
print('Hello')
When I try to run it in the shell all it does is print 'The Zen of Python' by Tim Peters, opens this webcomic in my browser (2 things I absolutely did not see coming) and throws the following error:
Traceback (most recent call last):
File "C:\Users\Hannah\Documents\dropBox\Python\test\test.py", line 1, in <module>
from Lib import *
AttributeError: 'module' object has no attribute 'crypt'
It takes a noticable amount of time before it does any of this, about 10-15 seconds
Maybe what you would like is a feature that automatically imports the libraries that are used in your script without needing to specify them at the beginning. I found this on the Internet http://www.connellybarnes.com/code/autoimp/
You just need one import at the beginning of your script
from autoimp import *
All other modules are loaded "lazily", i.e. when they are first used.
Example in the interactive shell:
>>> random.random()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'random' is not defined
>>> from autoimp import *
>>> random.random()
0.0679000238267422
From the docs:
For ultimate laziness, place the command "from autoimp import *" in your PYTHONSTARTUP file. Now your interactive session has all modules available by default.
Every module you import takes time to import. Importing every built-in module every time you start Python would kill performance in a lot of important scenarios where new Python interpreters are started frequently.
Python does have a set of modules that are always loaded, its call __builtins__ :).
Python's builtins provide the import statement for you to extend your scope with even more modules! But as other posts have said, deciding your script needs these modules it up to you. -- I have looked into mutating __builtins__ and I promise you, explicitly importing what you need is the better option.
((Big rant about not using from name import * cut from here))
Since most of writing python ultimately becomes packaging and installing that writen python somewhere, this is my goto set of resources for getting a handle on python's infamous import:
Start by sticking to standard tools and libraries (https://packaging.python.org/current/)
Reading and understand The Google Python Standards Guide (https://google.github.io/styleguide/pyguide.html),
Read the Zen of Python (https://www.python.org/dev/peps/pep-0020/)
Be Pythonic (basically adhere to "The Zen of Python"), https://www.youtube.com/watch?v=wf-BqAjZb8M
Supplement your problem-space with tips from The Hitchhikers Guid to Python (http://docs.python-guide.org/en/latest/)
Be preapred to package your code (https://packaging.python.org/distributing/ (Doc), https://github.com/pypa/sampleproject/ (Example))
Being prepared to debug someone else's and, Your own code by getting familiar with tools like:
pdb (import pdb; pdb.set_trace(), > pp variable),
print(help(variable)),
dir(variable),
and pprint.pprint( variable.__dict__ )