Python: No Module named xxx - python

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.

Related

unable to import module pyautogui

I am working on OS X
Ive looked at many different threads and haven't found a solution. Maybe I haven't been installing things properly from the start either. I want to import the module pyautogui into my project but it is saying it can't find it. I did a pip install in terminal and in the pycharmterminal as well. I have found the file directory and verified that it is available. But yet I am still not able to access it in my project. On some of the threads I read something about init.py, do I need to put that in my project. If so is there code in that py file or do I just create a python file named init.py
On Mac OS and some Linux distros, you may need to type pip3 install package-name.

how to fix the "__path__ attribute not found" error for packages installed by pip installer?

I recently installed the opencv package using pip install and I wrote a small code to test it (cvtest.py). The code runs through the python idle shell but running it though the command prompt gives the error
Error while finding module specification for 'cvtest.py' (ModuleNotFoundError: __path__ attribute not found on 'cvtest' while trying to find 'cvtest.py')
I tried uninstalling and reinstalling both python and the package. looking up the system path using python -m site gives these results. I am the only user of my laptop.
sys.path = [
'C:\\Users\\Kareem Mostafa\\Desktop\\Assignments\\computer vision',
'G:\\Python37\\python37.zip',
'G:\\Python37\\DLLs',
'G:\\Python37\\lib',
'G:\\Python37',
'G:\\Python37\\lib\\site-packages',
This is the code I am using
import cv2
x=cv2.imread('backpack for sale.jpg',0)
cv2.imshow('x',x)
update: the problem is happening with all the py files I am having whether they require imports or not. apparently python is looking for _init_.py for all the files as if they are packages. Any idea what is going on?
For anyone else that had this problem (assuming kareemostafa has fixed it now!)
Removing the .py suffix on the python -m command fixes this problem, it appears -m only requires module names whereas running it directly as a python file (no -m option) requires the .py suffix
In your case python -m cvtest should be sufficient.

Broken setup.py implementation for a minimal module

I forked the nice module multiscorer and I am trying to turn it into a package that I could install in different environments.
My fork can be found here. The steps I took are
Create a new environment (using conda) and activate it
python setup.py install from the root directory of the fork
In a new terminal, activate the environment and move to some arbitrary location. Start ipython and try from multiscorer import MultiScorer
I get the following error ImportError: cannot import name 'MultiScorer'. Note however, that import multiscorer works just fine. What do I have to change in the code to enable an installation using python setup.py install?
Another attempt: I tried to replace packages=['multiscorer'] with py_modules=['multiscorer.multiscorer']. Didn't help...
Your setup.py is alright. The problem is the package structure. Right now the correct way to import the Multiscorer class is this: from multiscorer.multiscorer import Multiscorer. The first multiscorer is for the folder (package) of the same name and the second multiscorer is for the multiscorer.py module inside the package.
The docs recommend putting all your code inside the __init__.py for such small packages.
If your codebase later grows too large for one file, you can start introducing other modules and use the __init__.py for exposing classes/functions on the package level.
Hope this helps.
It turns out I tried to import the wrong thing. The following: from multiscorer.multiscorer import MultiScorer works.
I am now wondering is this the pythonic way.

How to install a module on Python?

Okay, so, I'm actually a beginner in programming Python, and I only found out yesterday how you were supposed to encode pip install ModuleName in the Python command line and not in the interactive shell. I'm trying to download a lot of modules, such as the Send2Trash module, Pyperclip, Requests, Beautiful Soup, and Selenium.
Before I checked the forums about installing modules, I found out how we needed to have the pip tool. I'm a Windows user, but for some reason, I didn't have the 'Scripts' folder installed when I downloaded Python. I didn't know we needed it, so I used raw scripts from GitHub, setup.py, and copy pasted the script into the File Editor in Python, ran it in the interactive shell, and tried to import the module I needed. It worked for the Pyperclip and the Requests module; no errors popped up after I imported them using import pyperclip or import requests, but when I tried the same procedure for the rest of the modules I needed, there were some errors.
Also, when I tried to download the modules on pypi.python.org, I tried to open it using the interactive shell, but then something pops up, 'The file's encoding is invalid for Python3.x...', and when I click 'OK', it's going to say 'Failed to Decode', and close everything.
So, after reading forum after forum, I found out how to download pip, and was also able to download setuptools and wheel. I'm not sure if it's really already downloaded, but I was able to get the 'Scripts' folder that wasn't there before, so I guess so. I also already went into my PATH using the edit environment for your account thing, and I edited the Path variable so its value would lead to my 'Scripts' folder. Please do tell me if I did the right thing here.
So, following the advice of the forums, I tried to install the modules I needed by typing pip install ModuleName in the Python command line instead of the interactive shell, but it still gave me a Syntax Error. I also tried it in Command Prompt, typing the same code pip install ModuleName, but when I clicked Enter, nothing happens; no errors or anything. It seemed like my install was accepted, but when I tried importing the module in the interactive shell, it still gave an Import Error.
Please tell me what I did wrong throughout my process, and how to properly install the modules I need. I would include pictures into this, but it seems I can only add two before my reputation becomes 10, and I'm pretty new here, so... If there's anything I need to elaborate on about my problem, don't hesitate to ask, and I'll try my best.
You say you use windows so you need to understand pip.
pip is a program that installs python modules. You can even use easy_install instead of pip.
some pip commands
pip list -- lists out already installed modules.
pip search <module name> -- searches new modules.
pip -h -- more pip commands you want.
pip installs modules from CMD prompt not from python shell.
Even after installing modules some modules doesn't run as import module
they need to be imported as from module import function.
refer the pip help command and install modules.
DO NOT SAVE SCRIPT FILES IN PYTHON ROOT FOLDER YOU MAY FACE SOME PROBLEMS
Happy Programming!!!
After a whole lot of searching and trying out, I found the solution to my problem. For future Python users who encounter the same thing: always install your modules in the root folder.
In my case, my Command Prompt was automatically inside the C:\Users folder, which caused some problems because I couldn't download my module in there. Once I typed in cd C:\Python34, which was my root folder, I could successfully download the modules I needed using pip install ModuleName.

ImportError: No Module named 'mt_exceptions' when using Mingus library

Forgive my ignorance in advance, I am very new to python. I am trying to use a python 3 version (https://code.google.com/r/artdent-mingus-python3/) of the Mingus library (https://pypi.python.org/pypi/mingus/) in 3.4.2. When trying to use the note module, I keep getting the ImportError listed in the title.
Based on my research on similar ImportError questions, I have tried the following things:
-I made sure there was a mt_exceptions.py file in the relevant directory
-I made sure each mingus directory in the site-packages directory has init.py file
-I deleted the .pyc files in the mingus directory
Any pointers on what I might try next?
I've solved the problem by manually entering in "C:\Python34\Lib\site-packages\mingus\core" and editing the file (In my case, I had to edit notes.py from "from mt_exceptions import NoteFormatError, RangeError, FormatError" to "from mingus.core.mt_exceptions import NoteFormatError, RangeError, FormatError", and then I got a typical Python 2.x running on 3.x dict error. So that's the problem!
Solution: Use Python 2.x, or update the whole schmere to 3.x (That's what I'm doing).
As you can see here, Python 3.x has changed the import syntax a bit, and that's exactly what's happening. I had to run 2to3 through all the mingus source code and fix the imports manually from all files (Basically, porting the whole thing to Python 3.x).
Ericson Willians is correct. I fixed this with 2to3.py. I installed Mingus with:
pip install mingus
Then, I navigated to the directory where pip installed Mingus:
pip show mingus
In the top directory for Mingus (~/AppData/Local/Programs/Python/Python37-32/Lib/site-packages/mingus)
I ran:
~/AppData/Local/Programs/Python/Python37-32/Tools/scripts/2to3.py -w .
This has fixed Mingus for me, so far.

Categories