Use of installed libraries - python

A Python script starts with:
from pathlib import Path
import sqlite3
which I read as an initialization of Libraries needed to run the rest of the script. However if the following error is returned in the terminal:
ImportError: No module named pathlib
I am uncertain how to interpret this. One assumption is that the pathlib library is uninstalled. However on the local system Python 2.7 and Python 3.4 are installed (I believe one was system pre-installed).
How can a library be asserted to exist? In case it is missing, how can it be installed?

You have to install it first
pip install pathlib
And with that your code should work.

Related

Downloaded package ImportError

I have recently installed python 2.17.14 to use a package which I installed in the command prompt with:
python -m pip install packageName
However, whenever I try to use it with a script provided by the package authors, I get Import Errors:
ImportError: cannot import X from YX
ImportError: attempted relative import with no known parent package.
I don't know what I'm doing wrong, as I am really new to Python. Does anyone have any ideas?
The package is called neurodesign and I downloaded the try out script from the official website "neuropowertools.org"
Best,
Max
In case anyone (who is also new to python^^) fails ridiculously at this task as well: I had to manually install all the modules used within this package for it to work as they weren't installed automatically.

Error while importing pyVim.connect

I am attempting to run the following code which I received from samples here
from __future__ import print_function
import atexit
from pyVim.connect import SmartConnectNoSSL, Disconnect
from pyVmomi import vim
from tools import cli
I am receiving the following error:
ModuleNotFoundError: No Module named 'pyVim.connect'
The packages in question are from here and were installed using:
pip install pyvmomi
Is there something wrong with how I am installing these packages?
Looks like the code was a bit old. Importing 'pyvim' instead of 'pyVim' worked, though it seems to be named 'pyVim' on the github.
It's possible that you need to reinstall pvmomi to force the re-install of additional files in the pyVim/ package dir:
pip3 install --force pyvmomi
I haven't figured out how or what causes this, but the issue seems to occur on case-insensitive macOS file systems.
Since it has different behavior than Linux and case-sensitive macOS, I use the following "hack" to make it compatible between systems:
try:
from pyVim.connect import SmartConnectNoSSL
except ImportError:
from pyvim.connect import SmartConnectNoSSL
PS: You can use diskutil info / on macOS to figure out if your file system is case-sensitive or not (Details in another StackExchange question)

Python: No Module named xxx

Im getting nowhere with the following error on my Raspberry Pi:
My own Python script calls a function from another module named BlackBean.py which in turn imports other modules called "netaddr" and "configparser". The problem is that I just cant seem to get past the import error which tells me " No Module named netaddr, or if I comment out that import then it also errors with No Module named configparser. So I know its a path issue but I just cant seem to get it fixed!
The Blackbean.Py script starts like this:
import broadlink
import ConfigParser
import sys, getopt
import time, binascii
import netaddr
import BlackBeanSettings
import re
from os import path
from Crypto.Cipher import AES
SettingsFile = ConfigParser.ConfigParser()
SettingsFile.optionxform = str
SettingsFile.read(BlackBeanSettings.BlackBeanControlSettings)
def execute_command(etc.........
The BlackBean.py file is in my project SkyHD folder at /home/pi/SkyHD.
The "netaddr" and "configparser" files & folders were installed by pip in /home/pi/.local/lib/python2.7(and python3.5)/site-package folders.
sys.path has the above folders in its list and Ive also edited .bashrc and added PYTHONPATH=${PYTHONPATH}:/home/pi/.local/lib/python2.7/site-package:/home/pi/.local/lib/python3.5/site-package:/home/pi/SkyHD:../
but none of this works. I guess it must be something basic but I just cant work it out! help!
Also, some more info, when I first install all the files and run my program everything works fine and it finds the files ok with no problems, its only when I reboot it fails to find the files.
Its fixed.
Python looks for imported modules in 3 places, the first being the folder you launched the python script from; so for me the obvious answer is to import the modules I need directly into my own Project folder (/home/pi/myproject). This worked just fine, it works every time even after reboot, which was my main problem before. No need to create or alter PYTHONPATH, no need to mess around with entries in .bashrc or try to change the python path entries. Here are the steps:
Upgrade PIP to version 9.0.3 (not ver 10) with
pip install --upgrade pip==9.0.3
then install the required modules with the following
pip install --target=/home/pi/your_project_folder module_name
so for me it was... pip install --target=/home/pi/SkyHD netaddr
Im sure this is not best practice, but my Raspberry Pi only has this one project to run and having modules imported into the Projects folder just isnt an issue.
Hope this helps some others with the same problem.
You've provided insufficient information. Specifically, details about the python command being used to run your script such as its version (python -V) and its module search path if you do
env -u PYTHONPATH python -c 'import sys; print(sys.path);'
Similarly you can easily simplify the problem. What happens if you do python -m netaddr?
Obviously in the above commands substitute the actual python command being used to run your script.
And, as #BoarGules mentioned in his comments to your question, you should never, ever add directories to PYTHONPATH for different python versions unless you know that the modules in those directories has been written to work with python2 and python3.

How to import Python Libraries

I'm new to python and I'm using jython to create classes from python files.
I have jython and python both configured in my environment variables and when I type either in the command prompt I get their respective versions.
That's right I'm using windows :(
I need to create json output and was able to finally get the 'import json' to work and not give me an ImportError: No module name json.
Now I would like to import pysftp but I'm getting ImportError: No module named pysftp. Same thing with Paramiko import.
With that said I updated my jython to 2.7 and python version to 2.7.9 so I can use pip to import those libraries. Is this really what pip is for? How do I import library pysftp into my py file without it giving me the ImportError?
I'm a Java guy so I tried to locate pysftp jar and add it to the python Lib folder but that didn't work.
How do I import python libraries?

How to import a module with a dotted path?

I want to import the paramiko module located in /usr/local/lib/python2.7/dist-packages. So, I imported it this way:
from usr.local.lib.python2.7.dist-packages import paramiko
I have an error syntax related to python2.7 (It considers 7 as a package located in python2 package)
I have both Python3.1.3 and Python2.7 installed. I program with Python3.1.3 only, however.
How can I resolve this problem ?
How about ?
import sys
sys.path.append('/usr/local/lib/python2.7/dist-packages')
import paramiko
UPDATED
The best solution is installing paramiko on Python3 env. Take a look at #DanielRoseman's answer. Or virtualenv is worth consideration. Here is a good tutorial. http://simononsoftware.com/virtualenv-tutorial/
I don't know why you think you need to include the full path. That directory will already be included in the Python path. You just need to do import paramiko.
Edit after comment Well you can't randomly import things that are installed for a different version. There are several backwards incompatibilities, and anything that has any compiled extensions will just not work at all.
You need to download and install paramiko for your 3.1 installation, rather than trying to use the 2.7 version. python3 pip install paramiko, as an example.
(Also, you shouldn't really be using 3.1. If you're using the Python 3 series you should upgrade to 3.4.)

Categories