I am trying to run a python script which depends on other modules, however I ran into this:
bash-3.2$ PYTHONPATH=/my/path/tables-2.3.1/build/lib.linux-x86_64-2.7/ ./fastcluster.py
Traceback (most recent call last):
File "./fastcluster.py", line 5, in <module>
import tables
File "/my/path/tables-2.3.1/build/lib.linux-x86_64-2.7/tables/__init__.py", line 59, in <module>
from tables.utilsExtension import getPyTablesVersion, getHDF5Version
ImportError: libhdf5.so.7: cannot open shared object file: No such file or directory
bash-3.2$ ls libhdf5.so.7
libhdf5.so.7
bash-3.2$
No such file or directory libhdf5.so.7 ? But when I 'ls' it, it's there, right in my directory. So what's going on here?
The loader isn't looking there. Either put it in one of the standard locations for libraries, add the directory to the loader configuration, or set $LD_LIBRARY_PATH before running Python.
Related
This is my project structure:
- config
- data
- src
- resources
- db
- test
N.B.: I am using Python 3.9 and every folder that contains a .py file also has a __init__.py file
All the scripts I want to run are located in the /src folder and they used code from other scripts placed in the /src/resources folder (which is basically acting like a library).
Some of these scripts also read YAML files from the /config folder
Here is the problem, I cannot find a way to properly run these scripts from the command line, I am always getting errors like:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/runpy.py", line 185, in _run_module_as_main
mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
File "/usr/local/lib/python3.8/runpy.py", line 111, in _get_module_details
__import__(pkg_name)
File "/home/pi/crypto/src/ethMessage.py", line 4, in <module>
import update_db
File "/home/pi/crypto/src/update_db.py", line 1, in <module>
from db.mysql_main import insertValueAndFee
File "/home/pi/crypto/src/db/mysql_main.py", line 6, in <module>
from src.resources.parser import read_yaml
ModuleNotFoundError: No module named 'src'
I tried both with relative and absolute import, right now absolute import is what I am using (e.g. from src.resources.parser import read_yaml)
Which is the proper way to run scripts from the command line?
EDIT:
As you suggested, I added
sys.path.append( os.path.abspath(os.path.dirname(__file__)+'/..') )
to all the main scripts, and I am still getting a similar error:
Traceback (most recent call last):
File "src/ethMessage.py", line 6, in <module>
import update_db
File "/home/pi/crypto/src/update_db.py", line 1, in <module>
from db.mysql_main import insertValueAndFee
File "/home/pi/crypto/src/db/mysql_main.py", line 6, in <module>
from src.resources.parser import read_yaml
ModuleNotFoundError: No module named 'src'
To clarify, I am running my script from the global folder, which in my case is named "crypto".
I am also open to change the project structure with one that doesn't create problems.
If you want to refer to all of those packages by their root name, then all you have to do is add that folder to the Python path. So, for main program scripts in src, just add something like this:
import os
import sys
sys.path.append( os.path.abspath(os.path.dirname(__file__)+'/..') )
Now, the parent directory of your script will be on the path, no matter where you run it from. Now you can say
from src.resources.parser import read_yaml
If someone is still looking for a solution, I highly recommend not to bother with Python's imports: they are probably the worst part of the whole language.
Instead, if you want to use some files as a library, you should use setuptools to create a package from those files.
Then, you can install it locally or publish it on PyPi.
This way, you can import your library in a script just like another third-party module, (e.g. requests, selenium, ...), and things will work, instead of giving you a headache because a file is in a directory instead of another.
I have a python app that uses protocol buffers. I am trying to get it running in Docker, but it is throwing an error when I execute docker run:
Here are the Dockerfile contents:
FROM python:3.5-onbuild
CMD [ "python", "./main.py" ]
Here is the error:
bash-3.2$ docker run gcr.io/skroot-com/geo-boundary-refiner:0.1
Traceback (most recent call last):
File "./main.py", line 14, in <module>
from skroot.schema.pipeline.event import geo_boundary_marked_pb2
File "/usr/src/app/skroot/schema/pipeline/event/geo_boundary_marked_pb2.py", line 18, in <module>
from skroot.schema.pipeline import image_info_pb2 as skroot_dot_schema_dot_pipeline_dot_image__info__pb2
File "/usr/src/app/skroot/schema/pipeline/image_info_pb2.py", line 26, in <module>
dependencies=[skroot_dot_schema_dot_pipeline_dot_cloud__storage__location__pb2.DESCRIPTOR,skroot_dot_schema_dot_pipeline_dot___matrix__pb2.DESCRIPTOR,])
File "/usr/local/lib/python3.5/site-packages/google/protobuf/descriptor.py", line 829, in __new__
return _message.default_pool.AddSerializedFile(serialized_pb)
TypeError: Couldn't build proto file into descriptor pool!
Invalid proto descriptor for file "image_info.proto":
image_info.proto: Import "skroot/schema/pipeline/cloud_storage_location.proto" has not been loaded.
image_info.proto: Import "skroot/schema/pipeline/matrix.proto" has not been loaded.
skroot.schema.pipeline.ImageInfo.Image.location: "skroot.schema.pipeline.CloudStorageLocation" seems to be defined in "cloud_storage_location.proto", which is not imported by "image_info.proto". To use it here, please add the necessary import.
skroot.schema.pipeline.ImageInfo.TranscodedImage.matrix: "skroot.schema.pipeline.Matrix" seems to be defined in "matrix.proto", which is not imported by "image_info.proto". To use it here, please add the necessary import.
In the image_info.proto file, the .proto files it is complaining about are imported:
import "skroot/schema/pipeline/cloud_storage_location.proto";
import "skroot/schema/pipeline/matrix.proto";
The generated protobuf files are in the skroot/schema/pipeline/ directory.
I'm perplexed because this runs locally. It is only when I try to run it in Docker that I get these errors.
I have a project that I imported into pycharm. It looks like this:
The file grabber.py references class Fetcher within fetcher.py like this:
from grabber.fetcher import Fetcher
from the root folder Automaton I can run grabber.py from the command line with this command:
python -m grabber.grabber
But, I want to run grabber.py from pycharm. When I try I get this error:
Traceback (most recent call last):
File "C:/Automaton/grabber/grabber.py", line 1, in <module>
from grabber.fetcher import Fetcher
File "C:\Automaton\grabber\grabber.py", line 1, in <module>
from grabber.fetcher import Fetcher
ImportError: No module named 'grabber.fetcher'; 'grabber' is not a package
How do I get pycharm to run the file the same way I can from the command line?
My problem was pretty silly. I was having the problem because I had a package grabber and a file within it called grabber.py That name collision caused the problem. I renamed grabber.py and it works.
Im trying to figured out this problem. Yesterday I installed PyScripter and since then, scripts doesnt work. When I run any script (in PyScripter or IDLE) and trying to "import arcpy", it gets this error:
import arcpy
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import arcpy
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\__init__.py", line 17, in <module>
from geoprocessing import gp
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\__init__.py", line 14, in <module>
from _base import *
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py", line 14, in <module>
import arcgisscripting
File "C:\Python26\ArcGIS10.0\lib\arcgisscripting.py", line 81, in <module>
from RuntimeError import RuntimeError
ImportError: No module named RuntimeError
Have somebody any suggestion how to fix it?
Thanks to all very much!
Sounds like the module is not installed properly (or at all). To verify, do the following:
open a shell and start the python shell by typing python
(If this doesn't display an error, check that python is added to your system path or if it is even installed at all. But if you've used Python on said machine maybe system path issue)
enter the commmand help('modules')
review the list of modules that's returned to see whether arcpy is included
if not, then you may have to reinstall the module
I have a directory with two .py files in my C:\Python27\word_data called main.py and gethtml.py.
I want to import gethtml.py in my main.py, print def from that file, and I tried to do like this:
import gethtml
print gethtml.getHtmlText()
When I run this in a Python shell I get an error:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
execfile("word_rank/main.py")
File "word_rank/main.py", line 3, in <module>
import gethtml
ImportError: No module named gethtml
What am I missing?
You could check that the working directory for your python session is the directory containing your two python files. You can get Python to report the location of the current working directory as follows:
import os
print(os.getcwd())
Python will look in the current working directory (and in some directories in PATH) for the file you are trying to import. Not being able to find the file would give the error above.