I'm trying to embed python in to my application and have got stuck pretty early on.
I am embedding python into my C++ application and using the code found at this tutorial:
http://docs.python.org/2/extending/embedding.html#pure-embedding
My application matches entirely and compiles successfully no errors. However on running the application pModule = PyImport_Import(pName); line fails returning 0 meaning I get error output from PyErr_Print() of
Failed to load "C:\Users\workspace\dpllib\pyscript.py"
ImportError: Import by filename is not supported.
The application is being called with the commands C:\Users\workspace\ndnlib\pyscript.py multiply 50 150
I can't be sure, but I'm thinking that since pName is set to argv[1] and you're using the full path to call the script, then argv[1] is the full path. This means the code would try to import "C:\Users\workspace\dpllib\pyscript.py", which python can't (it can only import "pyscript").
Try running the script by just typing "pyscript.py" from within the directory and see if the error changes to 'Failed to load "pyscript.py"'. If it does, then you have to fix it so it doesn't just import argv[1] and modifies the string to get a module name instead of a file name.
It is simplier if you create your file as a module.
For instance,
create this:
<project>/MyModule/__init__.py
Then run your file <project>/script.py
dyn_module_name = (... get module name 'MyModule' from console arguments ...)
my_dynamic_module = __import__(dyn_module_name)
Since it would be a module, it will load while your scripy.py execution
Related
How do we get ctypes.CDLL("jvm.dll") to not find the dll and not give a "module not found" error?
We have verified the paths and can get them through file "open" commands of the jvm.dll - but the exact same paths when given to ctypes gives a "module not found" error.
We also tried editing the registry keys as per this answer.
We have also tried both running a 32 bit version of Python with a 32 bit version of Java as well as trying 64 bit versions.
We are working in Python 2.7.14 on Windows Server 2016.
Why could this be failing and what else can we try? Is there something preventing the Python script from seeing DLLs?
Here is more background:
The reason we are trying to get ctypes to work is because jpype is failing with the same error- and thus they seem foundationally related to the Python modules not being able to see or access the DLL.
When we run the following commands...
>> import ctypes
>> import os
>> os.chdir(r"C:\Program Files (x86)\Java\jre1.8.0_181\bin\client\")
>> ctypes.CDLL("jvm.dll")
...we get "module not found."
Yet, we can reach the file with open commands. For example, this call works from within Python (note it also works for the x86 path used above) to reach the jvm.dll:
>>> fo = open("C:\\Program Files\\Java\\jre1.8.0_181\\bin\\server\\jvm.dll")
But this call via Jpype fails even though is pathed to the same file:
>>> import jpype
>>> jpype.startJVM("C:\\Program Files\\Java\\jre1.8.0_181\\bin\\server\\jvm.dll")
Jpype gives the following error:
File "C:\Python27\lib\site-packages\jpype_core.py", line 70, in
startJVM
_jpype.startup(jvm, tuple(args), True) RuntimeError: Unable to load DLL [C:\Program Files\Java\jre1.8.0_181\bin\server\jvm.dll],
error = The specified module could not be found. at
native\common\include\jp_platform_win32.h:58
The reason we are using jpype is we are trying to get Jaydebeapi working to create a JDBC connection to a database.
How do we enable ctypes (and presumably thus jpype) to find and use the jvm.dll?
Our answer to solving this was to move from using the Python 2.7 interpreter to using Jython.
Using Jython, we were able to use the Jadebeapi library and connect to the database over JDBC.
I'm working on a large open source Python project, which has modules used by both the project and other projects. The goal is to move some of these modules out to a new "library" project that can then be imported by the original project and other projects.
To make this transition smooth, the thought was to copy the modules over to the new project, and have the original project then use the new import. However, to allow other project to have time to migrate later, the thought was to have the original module redirect the import.
For example, the usage is like this in repo 'neutron' (other projects could do the same):
cat neutron/consumer.py
from neutron.redirected import X
print(X)
The in the new 'neutron_lib' project created, the module looks like this (the same as what the original was in project 'neutron'):
cat ../neutron-lib/neutron_lib/redirected.py
X = 5
In the 'neutron' project, I'm trying to do this as the redirect module:
cat neutron/redirected.py
import neutron_lib.redirected
import sys
sys.modules['neutron.redirected'] = neutron_lib.redirected
When I run pylint, it gives these errors:
************* Module neutron.redirected
E: 1, 0: No name 'redirected' in module 'neutron_lib' (no-name-in-module)
************* Module neutron.consumer
E: 1, 0: No name 'X' in module 'neutron.redirected' (no-name-in-module)
If I run this, it runs fine, and consumer.py prints '5'. If I use ipython and load consumer.py, I can see 'X' in dir() output.
Any idea why I'm getting this pylint error? Is it a false error? Is there a way to override it?
Looks like, when running under tox, I can add the following to .pylintrc to hide the errors/warnings
no-name-in-module
nonstandard-exception
When I run pylint it passes now, as does running the Unit tests. Just wish I understood why I'm getting these errors/warnings though.
I'm having trouble running some codes that import cx_Oracle in command line, though the same codes work in console. Is there anything else I will need to set up in order to get this to work via command line please?
Saved just one line of code "import cx_Oracle" as test.py.
Run this line in ide (Spyder), iPython Notebook => no issues
run this by opening a command line window from the same folder the .py file is saved in, and run python test.py and encounter the below:
import cx_Oracle
ImportError: DLL load failed: %1 is not a valid Win32 application.
Not sure if there is anything additional I will need to set up to run cx_Oracle via command line? Have tried all the suggestions on setting PATH, ORACLE_HOME, re-installing but could not get this to work. versions that i'm using are
Python: 2.7
cx_Oracle: cx_Oracle-5.1.3-11g.win-amd64-py2.7
instant client: 12.1.0.0
Windows: 7 Enterprise
I also found this kind of problem.
Look at "not a valid Win32 application" this sentence, so I decide to change cx_Oracle to cx_Oracle-5.1.3-11g.win-32-py2.7. Luckly, it does work.
Here's my code configuration:
__init__py
./module_1/foo.py
./module_1/__init__.py
./module_2/bar.py
./module_2/__init__.py
My goal is to be able to import the foo module from bar.py, but I'm getting quite confused on how to do it.
Something such as:
from ..module_1.foo import *
Will raise the following error:
ValueError: Attempted relative import in non-package
EDIT:
Ideally I'd like to be able to run my script in the following fashion:
python ./module1/foo.py
You haven't shown how you are invoking the script, but you need to ensure that your scripts are actually packages in your python path. That's basically what the error message is telling you, you were trying to import a "non-package". You probably don't have your top-level in the python path. For example ...
If your top-level module is called app and your configuration is
<path-to-app>/app/__init__py
<path-to-app>/app/module_1/foo.py
<path-to-app>/app/module_1/__init__.py
<path-to-app>/app/module_2/bar.py
<path-to-app>/app/module_2/__init__.py
You can run your script as follows.
cd <path-to-app>
PYTHONPATH=$PWD python -m app.module_2.bar
Works for me.
I have written a python27 module and installed it using python setup.py install.
Part of that module has a script which I put in my bin folder within the module before I installed it. I think the module has installed properly and works (has been added to site-packages and scripts). I have built a simple script "test.py" that just runs functions and the script from the module. The functions work fine (the expected output prints to the console) but the script does not.
I tried from [module_name] import [script_name] in test.py which did not work.
How do I run a script within the bin of a module from the command line?
Are you using distutils or setuptools?
I tested right now, and if it's distutils, it's enough to have
scripts=['bin/script_name']
in your setup() call
If instead you're using setuptools you can avoid to have a script inside bin/ altogether and define your entry point by adding
entry_points={'console_scripts': ['script_name = module_name:main']}
inside your setup() call (assuming you have a main function inside module_name)
are you sure that the bin/script_name is marked as executable?
what is the exact error you get when trying to run the script? what are the contents of your setup.py?
Please check your installed module for using condition to checking state of global variable __name__.
I mean:
if __name__ == "__main__":
Global variable __name__ changing to "__main__" string in case, then you starting script manually from command line (e.g. python sample.py).
If you using this condition, and put all your code under this, it will be be work when you will try to import your installed module from another script.
For example (code from module will not run, when you will import it):
testImport.py:
import sample
...another code here...
sample.py:
if __name__ == "__main__":
print "You will never see this message, except of case, when you start this module manually from command line"