Failure to import module in python - python

I'm trying to use the twisted.mail package in Python:
root#beagleboard:~/twisted# ls /usr/lib/python2.6/site-packages/twisted/mail/
__init__.py imap4.pyo pop3client.py smtp.py
__init__.pyo mail.py pop3client.pyo smtp.pyo
_version.py mail.pyo protocols.py tap.py
_version.pyo maildir.py protocols.pyo tap.pyo
alias.py maildir.pyo relay.py test
alias.pyo pb.py relay.pyo topfiles
bounce.py pb.pyo relaymanager.py
bounce.pyo pop3.py relaymanager.pyo
imap4.py pop3.pyo scripts
I have twisted.mail installed, and within it is a module called imap4. twisted/mail contains the magic init.py file which makes it a python module.
so i should be able to import from it:
root#beagleboard:~/twisted# python
>>> from twisted.mail import imap4
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named twisted.mail
As you can see, I'm doing this on a Beagleboard running Angstrom, but that shouldn't matter, should it? However, I can do exactly the same thing on my Ubuntu 11.10 and it imports fine.
I have verified that I do not have a twisted.py module in my current directory.
What silly mistake am I making?

You're in the package that you're attempting to import. Go up one level first.

Related

In Python how do I correct this Module Not Found Error

I have the following module structure in my Administrator project folder
StockController.py (in the root project folder)
then in a package called Utilities I have the following two modules
ExcelManipulator.py
StockPurchaseInfo.py
Relevant Code from each module as as follows:
StockController.py
from Utilities.ExcelManipulator
import ExcelManipulatorClass
ExcelManipulator.py
from StockPurchaseInfo import StockInfoClass
class ExcelManipulatorClass:
StockPurchaseInfo.py
class StockInfoClass:
When running the StockController module I get the following error
Traceback (most recent call last):
File "d:\Administrator\StockController.py", line 1, in <module>
from Utilities.ExcelManipulator import ExcelManipulatorClass
File "d:\Administrator\Utilities\ExcelManipulator.py", line 2, in <module>
from StockPurchaseInfo import StockInfoClass
ModuleNotFoundError: No module named 'StockPurchaseInfo'
However when I run the ExcelManipulator.py module I dont get the error (it is able to find the StockPurchaseInfo module) why is this the case?
Do you have an __init__.py in your Utilities directory ?
And in your ExcelManipulator, you've to do this :
from Utilities.StockPurchaseInfo import StockInfoClass (or from .StockPurchaseInfo import StockInfoClass to import relatively)
If you run your programm from the root of your project (and you should do so), modules path are defined from it (from the root of the project), so if you're doing from StockPurchaseInfo import StockInfoClass then python is looking for an StockPurchaseInfo.py in you're root project.

Importing a python module to enable a script to be run from command line

I'm new to python and trying to test a script from this github repo (https://github.com/mgp25/psn-api).
The root directory has an example.py and I'm trying to run it with
$ python example.py
which gives this error:
Traceback (most recent call last):
File "example.py", line 1, in <module>
from src.Auth import Auth
ImportError: No module named src.Auth
How can I get this to run?
There is a folder in the root directory named src but because I'm new to python I don't know how to connect things so that the src.Auth module gets imported (or if that's even the right terminology)
Python 3.3+ will happily interpret it as a package without an __init__.py, fwiw, and I believe that's what the author wrote in.
Also note from trying to run it just now, you'll need to install simplejson and requests. (Normally there'd be a requirements.txt or similar saying this.)
Being in the repository root directory, you do:
touch src/__init__.py
This will create an empty file but it is necessary for the Python module search system. Then you should be able to run it without problems, unless there is some dependency on external libraries.

Can't import class from installed and imported module

I'm using Python 3.6 on windows, trying to get the py_mstr module installed so I can interact with a MicroStrategy web portal
I downloaded and installed the module by running it's "setup.py" and it appears to have properly installed it to to C:...\Python36-32\Lib\site-packages
I can import the module properly, but when I try to import a class, it returns an error
>>> import py_mstr
>>> from py_mstr import MstrClient
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
from py_mstr import MstrClient
ImportError: cannot import name 'MstrClient'
I checked and py_mstr definitely contains the class "MstrClient"
I dug around and found that folder structure might have something to do with it, but I can't figure it out
Python35-32
...
site-packages
...
py_mstr
__init__.py
py_mstr.py
_pycahce__
__init__.cpython-36.pyc
py_mstr.cpython-36.pyc
This package doesn't seem to support Python 3. You might be able to get it to run with 2to3, but if that doesn't work, you might have to run it on Python 2 instead.
The specific Python 3 incompatibility causing your import failure is that py_mstr/__init__.py uses an implicit relative import to bring in the contents of py_mstr/py_mstr.py:
from py_mstr import *
Python 3 interprets this as importing * from the py_mstr package, not the py_mstr.py_mstr submodule. There may be other incompatibilities; I didn't do a thorough inspection.

ModuleNotFoundError: No module named 'futval.py'; 'futval' is not a package

I am a beginner with Python and using Python 3.6.0 on Mac OS X 10.12. I imported a module saved in my documents folder with all of my other modules that worked fine, yet it came back with this after the module ran:
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
import futval.py
ModuleNotFoundError: No module named 'futval.py'; 'futval' is not a package
All of my other modules ran fine before this one and I can't figure out the issue. I've searched around the site for answers and can't seem to find anything. Any help is appreciated.
If you're trying to import a file named futval.py, you need to write just import futval; the name of the module does not include the .py.

import error in python on ubuntu

I have created my first python module on ubuntu. When I'm trying to import the module in python using :
import brian
it is giving error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named brian
I have brian in /home/noamaan and python is in /usr/bin.
If you launch python from the directory that contains brian module, everything will work as it is now.
To import custom module from anywhere you want you should read attentively something on the import mechanism in python to learn where the imported modules are searched for, etc.
But to make your code work right now, I can recommend you the following:
Either extend your PYTHONPATH variable before running python, to include the directory of your module
Or append it right in the code by using sys module in this way.
import sys
sys.path.append("path/to/module/dir")
import brian
Also, see info on site module
by default Python import modules from Python path var.
You can view these paths so:
import sys
print sys.path

Categories

Resources