ImportError: No module named paramiko - python

I have installed "python-paramiko" and "python-pycrypto" in Red hat linux.
But still when i run the sample program i get "ImportError: No module named paramiko".
I checked the installed packages using below command and got confirmed.
ncmdvstk:~/pdem $ rpm -qa | grep python-p
python-paramiko-1.7.6-1.el3.rf
python-pycrypto-2.3-1.el3.pp
My sample program which give the import error:
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(
paramiko.AutoAddPolicy())
ssh.connect('127.0.0.1', username='admin',
password='admin')

Actually all these packages were installed outside the python folder.
And all I did was linking the packages from python folder to packages folder.
It worked perfectly.

Related

How to distribute and or package a python file with all dependencies pre-downloaded without building an executable

I have written a python script which depends on paramiko to work. The system that will run my script has the following limitations:
No internet connectivity (so it can't download dependencies on the fly).
Volumes are mounted with 'noexec' (so I cannot run my code as a binary file generated with something like 'pyInstaller')
End-user cannot be expected to install any dependencies.
Vanilla python is installed (without paramiko)
Python version is 2.7.5
Pip is not installed either and cannot be installed on the box
I however, have access to pip on my development box (if that helps in any way).
So, what is the way to deploy my script so that I am able to provide it to the end-user with the required dependencies, i.e paramiko (and sub-dependencies of paramiko), so that the user is able to run the script out-of-the-box?
I have already tried the pyinstaller 'one folder' approach but then faced the 'noexec' issue. I have also tried to directly copy paramiko (and sub-dependencies of paramiko) to a place from where my script is able to find it with no success.
pip is usually installed with python installation. You could use it to install the dependencies on the machine as follows:
import os, sys
def selfInstallParamiko():
# assuming paramiko tar-ball/wheel is under the current working directory
currentDir = os.path.abspath(os.path.dirname(__file__))
# paramikoFileName: name of the already downloaded paramiko tar-ball,
# which you'll have to ship with your scripts
paramikoPath = os.path.join(currentDir, paramikoFileName)
print("\nInstalling {} ...".format(paramikoPath))
# To check for which pip to use (pip for python2, pip3 for python3)
pipName = "pip"
if sys.version_info[0] >= 3:
pipName = "pip3"
p = subprocess.Popen("{} install --user {}".format(pipName, paramikoPath).split())
out, err= p.communicate("")
if err or p.returncode != 0:
print("Unable to self-install {}\n".format(paramikoFileName))
sys.exit(1)
# Needed, because sometimes windows command shell does not pick up changes so good
print("\n\nPython paramiko module installed successfully.\n"
"Please start the script again from a new command shell\n\n")
sys.exit()
You can invoke it when your script starts and an ImportError occurs:
# Try to self install on import failure:
try:
import paramiko
except ImportError:
selfInstallParamiko()

cx_Oracle error. DPI-1047: Cannot locate a 64-bit Oracle Client library

I installed the library and when trying to access SQL in jupyter notebook with my credentials the following error appears:
DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "The specified module could not be found". See https://oracle.github.io/odpi/doc/installation.html#windows for help
The easiest solution is as follows:
Download 64-bit version of oracle instantClient from: https://www.oracle.com/database/technologies/instant-client/winx64-64-downloads.html
Copy the dll files in the instantclient directory to the python directory, as shown below
That is it!
The short answer is: cx_Oracle.init_oracle_client(lib_dir= r"c:\path_to_libraries")
Here are the steps that I followed to solve this same issue:
If you have not already installed cx_Oracle you can do so with the following command:
python -m pip install cx_Oracle --upgrade
The cx_Oracle documentation can be found here.
Use the following commands to verify that everything is installed and recognized:
import sqlalchemy as sqla
import pandas as pd
import cx_Oracle
# Test to see if it will print the version of sqlalchemy
print(sqla.__version__) # this returns 1.2.15 for me
# Test to see if the cx_Oracle is recognized
print(cx_Oracle.version) # this returns 8.0.1 for me
# This fails for me at this point but will succeed after the solution described below
cx_Oracle.clientversion()
At this point I get errors saying the libraries cannot be located. Here is the solution:
import os
import platform
# This is the path to the ORACLE client files
lib_dir = r"C:\put_your_path_here\instantclient-basic-windows.x64- 19.9.0.0.0dbru\instantclient_19_9"
# Diagnostic output to verify 64 bit arch and list files
print("ARCH:", platform.architecture())
print("FILES AT lib_dir:")
for name in os.listdir(lib_dir):
print(name)
Be sure to update the lib_dir path specific to your installation. If you have the correct path, you should see a listing of all the Oracle files like: (adrci.exe, oci.dll, oci.sym, etc). This is the location that Python needs to be able to find the Oracle drivers.
The current (Nov 2020) standard way for passing the location of the Oracle libraries for Windows is cx_Oracle.init_oracle_client(lib_dir= r"c:\path_to_libraries"). Here is an example:
lib_dir = r"C:\put_your_path_here\instantclient-basic-windows.x64- 19.9.0.0.0dbru\instantclient_19_9"
try:
cx_Oracle.init_oracle_client(lib_dir=lib_dir)
except Exception as err:
print("Error connecting: cx_Oracle.init_oracle_client()")
print(err);
sys.exit(1);
At this point I can run the following code without any errors:
# This works after passing the lib_dir path
cx_Oracle.clientversion() # For me it returns: (19, 9, 0, 0, 0)
DEPRECATED Here is how to update the PATH variable temporarily:
The following works, but using cx_Oracle.init_oracle_client(lib_dir= r"c:\path_to_libraries") is now the preferred way.
import os
# Manually append the location of the ORACLE libraries to the PATH variable
os.environ["PATH"] = lib_dir + ";" + os.environ["PATH"]
As per documentation accessed on cx_Oracle page.
Step 1: install cx_Oracle
python -m pip install cx_Oracle --upgrade
Step 2: Download and extract Oracle Basic Client
For Windows download and extract Oracle Basic Instatnt client instantclient-basic-windows.x64-19.9.0.0.0dbru.zip file.
Step 3: Inform cx_Oracle module about the Instatnt Client location.
If you stick to documentation and extract them in c:\oracle folder then your script may look like this.
import cx_Oracle
cx_Oracle.init_oracle_client(lib_dir=r"C:\oracle\instantclient_19_9")
Now you will be free from the error.
I faced this error in Anconda Spyder.
This is how I fixed it.
Get the basic package instantClient from: https://www.oracle.com/database/technologies/instant-client/winx64-64-downloads.html
Extract and copy all the *.dll files and paste it to Anaconda3 folder where you have python.exe.
For MAC
After you do : python -m pip install cx_Oracle --upgrade
try:
import cx_Oracle
# Test to see if the cx_Oracle is recognized
print(cx_Oracle.version) # this returns 8.0.1 for me
# This fails for me at this point but will succeed after the solution described below
cx_Oracle.clientversion()
if you encounter issues like :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle Client library: "dlopen(libclntsh.dylib, 1): image not found". See https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html for help
Follow the step highlighted here: https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html#manual-installation
Manual Installation
Download the Basic 64-bit DMG from Oracle.
(https://www.oracle.com/database/technologies/instant-client/macos-intel-x86-downloads.html)
Mount the dng
Open the mounted dng, and run ./install_ic.sh (via terminal)
e.g. : cd /Volumes/instantclient-basic-macos.x64-19.8.0.0.0dbru/ && ./install_ic.sh
In Finder, eject the mounted Instant Client package.
Re-run the oracle connection.
import cx_Oracle
cx_Oracle.init_oracle_client(lib_dir="/Users/priyank.pathak/Downloads/instantclient_19_8")
## cx_Oracle.init_oracle_client(lib_dir="/Users/your_username/Downloads/instantclient_19_8")
cx_Oracle.clientversion()
Make sure you have installed the correct oracle client (you can find the oracle client package from here "https://www.oracle.com/in/database/technologies/instant-client/winx64-64-downloads.html" and add the downloaded folder inside the folder where python is installed and then add this location (location of your client package folder) to system's environment variable.
Hope that will work.
I suggest you to first check the compatibility of your OS, Python and Oracle Instant Client architecture:
import platform
platform.architecture()
Then, I definitely advise you to set the Oracle Instant Client inside your jupyter notebook:
import os
os.environ["PATH"] = "Complete Location of Instant Client Folder" + ";" + os.environ["PATH"]
Probably you have installed cx_Oracle Python library. In order to execute the DB connectivity through jupyter notebook, you need to install the Oracle Client and that is what missing in your scenario. Please follow the steps from below link and and install Oracle Client and this will solve your problem:
https://oracle.github.io/odpi/doc/installation.html#windows
I went into the same problem but on Arch Linux, running in NodeJS.
Some of the answers here helped me in searching for the official installation guide.
Then I followed these steps and they worked for me:
https://oracle.github.io/node-oracledb/INSTALL.html#instzip , hope it could help someone else too.
Download Instant Client Basic Light Package (ZIP) (Linux)
https://www.oracle.com/database/technologies/instant-client/linux-x86-64-downloads.html
Create folder and unzip into it
mkdir /opt/oracle
unzip ~/Downloads/instantclient-basiclite-linux.x64-21.4.0.0.0dbru.zip -d /opt/oracle/
Then, according to the docs, execute these commands below:
If there is no other Oracle software on the machine that will be impacted, then permanently add Instant Client to the run-time link path. For example, if the Basic package unzipped to /opt/oracle/instantclient_19_11, then run the following using sudo or as the root user
sudo sh -c "echo /opt/oracle/instantclient_19_11 > /etc/ld.so.conf.d/oracle-instantclient.conf"
sudo ldconfig
OR
Alternatively, every shell running Node.js will need to have the link path set:
export LD_LIBRARY_PATH=/opt/oracle/instantclient_19_11:$LD_LIBRARY_PATH
This might help someone:
I also had the same error and I read through the installation documentation https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html (I was using a zip for installation so I focused on section: Oracle Instant Client Zip Files)
And I was missing a "yum install -y libaio" (used 'centos:7' base image) after unzipping the oracle client to /opt/oracle and also added the following envs:
ENV ORACLE_HOME /opt/oracle/instantclient_19_3
ENV DYLD_LIBRARY_PATH /opt/oracle/instantclient_19_3
ENV LD_LIBRARY_PATH /opt/oracle/instantclient_19_3
ENV TNS_ADMIN /opt/oracle/instantclient_19_3
I just installed Sql Developer from Oracle and ran it for the first time, and the install added what was needed and I was able to get through that error. This was on windows. Install below, just extract it when it installs and run the .exe in the main folder. Much simpler than the other solutions!
https://www.oracle.com/database/sqldeveloper/technologies/download/
You just need to put the Oracle Instant Client folder in your PATH environment variable. Remember to restart whatever application that needs the instant client after updating the PATH variable.
If you installed the instant client at: c:\oracle\instantclient_21_8, simply put this folder in your PATH environment variable.
Copying the instant client files to Python folder is a very bad idea, since if you have other python versions installed in the future you will have to remember duplicating de instant client files. If you upgrade the instant client, you will need to keep these duplicated files updated too.

Where are raspberryPi python packages installed?

I was given a raspberry pifor my birthday and decided to make an alarm clock out of one. I wrote all the code on my PC, works completely fine and expected but I'm having slight issues with installing packages on the raspberry pi.
When I open the terminal, I'm in the directory "home/pi".
I then run the command
sudo easy_install -U schedule
which installs fine, I then try to run my code which is stored in "home/pi", but get an error on:
Traceback (most recent call last):
File "/home/pi/LED.py", line 1, in <module>
import schedule
ImportError: No module named 'schedule'
any tips? I've also installed schedule via pip in the same directory - pip install schedule which installs perfectly fine.
#!/usr/bin/python
import schedule
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, GPIO.HIGH)
time.sleep(5)
GPIO.output(18, GPIO.LOW)
GPIO.cleanup()
Python searches the packages in all directories in the python path
For instance, these directories for me are :
>>> import sys
>>> sys.path
['', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/lib/python3.6/site-packages']
Note that the first path is "the directory containing the script that was used to invoke the Python interpreter", often the current directory when you run your python script.
Also note that pip should install the packages in the site-packages directory. (The last path in my sys.path in my previous example). There should be one of these directory per python installation.
A simple command line like find / -name site-packages should be enough to find them all. But keep in mind that not all python interpreter will use the same sys.path : obviously, if you install some package for python2, you won't be able to access it from a python3 interpreter. Same thing if you use virtualenvs.
just ran into the same problem and on my raspberry 4 it helped when I used the following from the command line where the python script was launched:
sudo python3 example.py
Note the "3" at the end of "python" above.
i came across this when searching for an answer. and then i was just testing something that got me thinking about where the packages.
i have had probles using "sudo python something.py".
getting the "ImportError: No module named "some module"
i have always been using "pip install" or "pip3 install", and always could have used it as my user, but been as i wrote, problem when i "sudo python something.py"
what fixed it in my case, is install the module again with "sudo pip install" or sudo pip3 install". though then the modules would be installed 2 places i guess. though i havent tested how it would be reachable for a normal user yet...

ImportError: No module named 'thread'

when I run mitmproxy command in command line, I get the following error.
% mitmproxy
Traceback (most recent call last):
File "/usr/local/bin/mitmproxy", line 7, in <module>
from libmproxy.main import mitmproxy
File "/usr/local/lib/python3.5/site-packages/libmproxy/main.py", line 5, in <module>
import thread
ImportError: No module named 'thread'
I googled this error and found this stackoverflow Q&A page.
pydev importerror: no module named thread, debugging no longer works after pydev upgrade
according to the page above, the error occurs because module "thread" is renamed to "_thread" in python3.
So, I know what's causing this error, but then what?
I don't know what to do now in order to get rid of this error.
I'm new to python. I've just installed Python and pip into my mac OSX as shown below because I want to use mitmproxy.
% which pip
/usr/local/bin/pip
% pip --version
pip 8.1.1 from /usr/local/lib/python3.5/site-packages (python 3.5)
% which python
/usr/bin/python
% which python3
/usr/local/bin/python3
% python --version
Python 2.7.10
% python3 --version
Python 3.5.1
could anyone please tell me what to do now?
Additional Info
As #linusg answered, I created "thread.py" file in "site-packages" directory and pasted the code below in "thread.py"
from _thread import *
__all__ = ("error", "LockType", "start_new_thread", "interrupt_main", "exit", "allocate_lock", "get_ident", "stack_size", "acquire", "release", "locked")
After I did this, "ImportError: No module named 'thread'" disappeared, but now I have another ImportError, which is "import Cookie ImportError: No module named 'Cookie'".
It seems that in Python 3, Cookie module is renamed to http.cookies (stackoverflow.com/questions/3522029/django-mod-python-error).
Now what am I supposed to do?
What I have in "site-packages" directory
% ls /usr/local/lib/python3.5/site-packages (git)-[master]
ConfigArgParse-0.10.0.dist-info/ mitmproxy-0.15.dist-info/
OpenSSL/ netlib/
PIL/ netlib-0.15.1.dist-info/
Pillow-3.0.0.dist-info/ passlib/
PyYAML-3.11.dist-info/ passlib-1.6.5.dist-info/
__pycache__/ pathtools/
_cffi_backend.cpython-35m-darwin.so* pathtools-0.1.2.dist-info/
_markerlib/ pip/
_watchdog_fsevents.cpython-35m-darwin.so* pip-8.1.1.dist-info/
argh/ pkg_resources/
argh-0.26.1.dist-info/ pyOpenSSL-0.15.1.dist-info/
backports/ pyasn1/
backports.ssl_match_hostname-3.5.0.1.dist-info/ pyasn1-0.1.9.dist-info/
blinker/ pycparser/
blinker-1.4.dist-info/ pycparser-2.14.dist-info/
certifi/ pyparsing-2.0.7.dist-info/
certifi-2016.2.28.dist-info/ pyparsing.py
cffi/ pyperclip/
cffi-1.6.0.dist-info/ pyperclip-1.5.27.dist-info/
click/ setuptools/
click-6.2.dist-info/ setuptools-19.4-py3.5.egg-info/
configargparse.py sitecustomize.py
construct/ six-1.10.0.dist-info/
construct-2.5.2.dist-info/ six.py
cryptography/ test/
cryptography-1.1.2.dist-info/ thread.py
easy_install.py tornado/
hpack/ tornado-4.3.dist-info/
hpack-2.0.1.dist-info/ urwid/
html2text/ urwid-1.3.1.dist-info/
html2text-2015.11.4.dist-info/ watchdog/
idna/ watchdog-0.8.3.dist-info/
idna-2.1.dist-info/ wheel/
libmproxy/ wheel-0.26.0-py3.5.egg-info/
lxml/ yaml/
lxml-3.4.4.dist-info/
In Python 3 instead of:
import thread
Do:
import _thread
You are trying to run Python 2 code on Python 3, which will not work.
As of April 2016, mitmproxy only supports Python 2.7. We're actively working to fix that in the next months, but for now you need to use Python 2 or the binaries provided at http://mitmproxy.org.
As of August 2016, the development version of mitmproxy now supports Python 3.5+. The next release (0.18) will be the first one including support for Python 3.5+.
As of January 2017, mitmproxy only supports Python 3.5+.
Go to you site-packages folder, create a file called thread.py and paste this code in it:
from _thread import *
__all__ = ("error", "LockType", "start_new_thread", "interrupt_main", "exit", "allocate_lock", "get_ident", "stack_size", "acquire", "release", "locked")
This creates an 'alias' for the module _thread called thread. While the _thread module is very small, you can use dir() for bigger modules:
# Examle for the Cookies module which was renamed to http.cookies:
# Cookies.py in site-packages
import http.cookies
__all__ = tuple(dir(http.cookies))
Hope this helps!
Easiest solution is to create a virtualenv with python2 and run mitmproxy on this virtualenv
virtualenv -p `which python2` .env
source .env/bin/activate
pip install mitmproxy
.env/bin/mitmproxy
The name of the file saved could be threading, this would give an error as threading is a predefined class in Python. Try changing the name of your file. It would help....

google ad exchange request.py run error in eclipse

When debugging my "google dobule click exchange testing script", it runs successfully on a Python Terminal, but when doing so with the eclipse pydev IDE, the following line give me an error:
import realtime_bidding_pb2
The error I'm getting is
ImportError: No module named realtime_bidding_pb2
I know this module is outsourced, but I don't know where it is or how to import it into the eclipse pydev IDE.
If you use unix like OS, change directory to the directory where the files requester.py, Makefile, realtime-bidding.proto (from requester.tar.gz) and execute command 'make' in shell. realtime_bidding_pb2 file will be created.
Next you get "ImportError: No module named google.protobuf". You need install python-protobuf (command 'apt-get install python-protobuf' for Ubuntu, you should be root).
Uses requester.py by command like 'python requester.py --url= --max_qps=1 --seconds=20'.

Categories

Resources