Directory structure:
Application/
proto/
payload.proto
lab_account.proto
public_trail.proto
protocompiled/
payload_pb2.py
lab_account_pb2.py
Contents of the payload.proto
syntax = "proto3";
import "lab_account.proto";
import "public_trail.proto";
if I compile my payload.proto file, with command .
Applicationā« protoc --proto_path=./proto --python_out=./protocompiled payload.proto
The compiled payload_pb2.py doest have the required imports. It has wrong import statement like this.
import lab_account_pb2 as lab__account__pb2
import public_trail_pb2 as public__trail__pb2
instead of this;
import protocompiled.lab_account_pb2 as lab__account__pb2
import protocompiled.public_trail_pb2 as public__trail__pb2
Also referred, https://github.com/protocolbuffers/protobuf/issues/1491 but couldnt solve the issue.
There is an open issue 5374 in protobuf github regarding this problem.
Until it is resolved, I use the following workaround: after protoc run a sed script (works at least for GNU sed) which will add relative imports.
protoc $PATH/*.proto --python_out=$PROTOC_OUTDIR
sed -i $PROTOC_OUTDIR/*_pb2.py -e 's/^import [^ ]*_pb2/from . \0/'
But this works only when all files are in the same directory. More complex script will be required to handle files spread over multiple directories.
Related
So I am trying to use this repository on my computer but I cant seem to import a local module (Agent.pyx) and use functions from that file. The error I get is:
ModuleNotFoundError: No module named 'RLAgent.Agent
Screenshot of error
Link to repository
You have to install the package manually
Since Agent.pyx is a .pyx file, you will need to first build it. The README file in the linked repo also mentions this. So you need to move to the RLAgent directory and execute the setup instruction as:
cd RLAgent
python3 setup.py build_ext --inplace
This will build the required .c and .so file which will then enable it's import in other files such as Train.py which is where the import is throwing an error.
To install a package that includes a setup.py file, usually you cd into the root directory where setup.py is located and then run the following:
python setup.py install
Now my problem is that I'm building an addon for another application, and while I can install "normal" package via code with the following:
subprocess.run([sys.executable, "-m", "pip", "install", package])
I also have to install a couple custom made packages, and if I try to do it like this:
subprocess.run([sys.executable, "-m", "/MSN-Point-Cloud-Completion-master/emd/setup.py", "install"])
I get the following error: Error while finding module specification for 'MSN-Point-Cloud-Completion-master/emd/setup.py' (ModuleNotFoundError: No module named 'MSN-Point-Cloud-Completion-master/emd/setup')
What is annoying me is that if I manually cd into the directory and simply run python setup.py install from the cmd it works just fine.
So the problem seems to be passing the relative path to the setup.py file, but as I said, considering this is an addon I need to install that setup.py from code. Any solution?
Reading comments made me realize a few mistakes.
First of all I realized that I shouldn't use -m flag in this case and this allow me to execute setup.py correctly. Also deleted the extra slash in the path and now the following works:
subprocess.run([sys.executable, "MSN-Point-Cloud-Completion-master/emd/setup.py", "install"])
However, since I wasn't in that directory still, I also got the following error c1xx: fatal error C1083: It is not possible to open file: 'emd_cuda.cu': No such file or directory and the compilation failed.
This definitely fixed all the problems:
import os
import subprocess
os.chdir("MSN-Point-Cloud-Completion-master/emd/")
subprocess.run([sys.executable, "setup.py", "install"])
I would like to use the maraboupy python package on a Kaggle notebook. I have tried this:
!git clone https://github.com/NeuralNetworkVerification/Marabou.git
import sys
sys.path.insert(1, '/kaggle/working/Marabou')
But when I try the following code, I get
'ModuleNotFoundError: No module named 'maraboupy.MarabouCore'
from maraboupy import Marabou
Here is the installation guide
Based on the installation guide, I have tried the following commands:
mkdir build
cd build
cmake .. -DBUILD_PYTHON=ON
cmake --build .
I got this:
CMake Error at CMakeLists.txt:239 (target_include_directories): Cannot specify include directories for imported target "openblas".
Help me to install maraboupy on Kaggle.
I don't know the details, but I've got maraboupy working in a different set-up.
From the CMake Error it seems that you are running the installation commands in '/kaggle/working' if so, try running the commands in '/kaggle/working/Marabou' this is where my CMakeLists.txt is at least.
Another difference I can spot is that I added Marabou to path as well, don't know if that is important. In your case I suppose this would be:
!git clone https://github.com/NeuralNetworkVerification/Marabou.git
import sys
sys.path.insert(1, '/kaggle/working/Marabou/maraboupy')
sys.path.insert(2, '/kaggle/working/Marabou')
I am trying to install pdfMiner to work with CollectiveAccess. My host (pair.com) has given me the following information to help in this quest:
When compiling, it will likely be necessary to instruct the
installation to use your account space above, and not try to install
into the operating system directories. Typically, using "--
home=/usr/home/username/pdfminer" at the end of the install command
should allow for that.
I followed this instruction when trying to install.
The result was:
running install
running build
running build_py
running build_scripts
running install_lib
running install_scripts
changing mode of /usr/home/username/pdfminer/bin/latin2ascii.py to 755
changing mode of /usr/home/username/pdfminer/bin/pdf2txt.py to 755
changing mode of /usr/home/username/pdfminer/bin/dumppdf.py to 755
running install_egg_info
Removing /usr/home/username/pdfminer/lib/python/pdfminer-20140328.egg-info
Writing /usr/home/username/pdfminer/lib/python/pdfminer-20140328.egg-info
I don't see anything wrong with that (I'm very new to python), but when I try to run the sample command $ pdf2txt.py samples/simple1.pdf I get this error:
Traceback (most recent call last): File "pdf2txt.py", line 3, in <module>
from pdfminer.pdfdocument import PDFDocument ImportError: No module named pdfminer.pdfdocument
I'm running python 2.7.3. I can't install from root (shared hosting). The most recent version of pdfminer, which is 2014/03/28.
I've seen some posts on similar issues ("no module named. . . " but nothing exactly the same. The proposed solutions either don't help (such as installing with sudo - not an option; specifying the path for python (which doesn't seem to be the issue), etc.).
Or is this a question for my host? (i.e., something amiss or different about their setup)
I had an error like this:
No module named 'pdfminer.pdfinterp'; 'pdfminer' is not a package
My problem was that I had named my script pdfminer.py which for the reasons that I don't know, Python took it for the original pdfminer package files and tried to compiled it.
I renamed my script to something else, deleted all the *.pyc file and __pycache__ directory and my problem was solved.
use this command worked for me and removed the error
pip install pdfminer.six
Since the package pdfminer is installed to a non-standard/non-default location, Python won't be be able to find it. In order to use it, you will need to add it to your 'pythonpath'. Three ways:
At run time, put this in your script pdf2txt.py:
import sys
# if there are no conflicting packages in the default Python Libs =>
sys.path.append("/usr/home/username/pdfminer")
or
import sys
# to always use your package lib before the system's =>
sys.path.insert(1, "/usr/home/username/pdfminer")
Note: The install path specified with --home is used as the Lib for all packages which you might want to install, not just this one. You should delete that folder and re-install with --
home=/usr/home/username/myPyLibs (or any generic name) so that when you install other packages with that install path, you would only need the one path to add to your local Lib to be able to import them:
import sys
sys.path.insert(1, "/usr/home/username/myPyLibs")
Add it to PYTHONPATH before executing your script:
export PYTHONPATH="${PYTHONPATH}:/usr/home/username/myPyLibs"
And then put that in your ~/.bashrc file (/usr/home/username/.bashrc) or .profile as applicable. This may not work for programs which are not executed from the console.
Create a VirtualEnv and install the packages you need to that.
I have a virtual environment and I had to activate it before I did a pip3 install to have the venv see it.
source ~/venv/bin/activate
I have found most python modules in python source directory, under Python/Lib or Python/Modules ,but where is the sys (import sys) module ? I didn't find it .
The Answer
I find it here: ./Python/sysmodule.c
If you're on Linux or Mac OS X, and in doubt, just try find . -name 'sysmodule.c' in the Python directory.
Other Stuff
The way I found it was by searching for the string "platform" throughout the Python directory (using TextMate), as I've used e.g. sys.platform before from the sys module... something similar can be done with grep and xargs.
Another suggestion could be : for i in ./**/*.c ; do grep -H platform $i ; done
This will loop through all *.c files present in anywhere up the file tree you're currently located at, and search the file for "platform". The -H flag will ensure we get a filename path so we can trace the matches back to the files they are found in.
import sys
help(sys)
Then you will see something like the following:
Help on built-in module sys:
NAME
sys
FILE
(built-in)
In my working environment, sys is built into python itself.
It's in Python/Python/sysmodule.c.