Can't import class from installed and imported module - python

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.

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.

How to import my python file in another file in visual studio code

I am using python/selenium in visual studio code. I am trying to import my another python class driverScript which resides in executionEngine module and in the file DriverScript. I have imported as below:
import driverScript from executionEngine.DriverScript
It is producing error:
Traceback (most recent call last):
File "c:/Selenium/Selenium-Python Framework/RK_Practice/Tests/mainTest.py", line 5, in <module>
from executionEngine.DriverScript import driverScript
ModuleNotFoundError: No module named 'executionEngine'
How can I import correctly? Your help is very much appreciated.
If the script you are wishing to import is not in the current directory, you may want to take this approach:
import sys
sys.path.insert(1, '/path/to/script/folder')
import driverScript from executionEngine.DriverScript
If your python file is on the same level in dir, then you can import just by calling:
import filename
If your python file is inside another folder, then you need to create a blank __init__.py file that will help python to understand it's a package. Then you can import that as follows:
from folderName import filename
Depends on where executionEngine is supposed to come from. If it's from a package installable via a package manager such as pip or Anaconda, looks like it's not properly installed. If you installed it yourself, you probably need to add the directory containing executionEngine to your PYTHONPATH, so the Python interpreter can find it. This can be done in the VSCode environment files. See the PYTHONPATH section in https://code.visualstudio.com/docs/python/environments

pybedtools: ImportError: cannot import name scripts

I am trying to import Pybedtools in Spyder.
from pybedtools import BedTool
This is the error I am getting:
Traceback (most recent call last):
File "<ipython-input-13-7a8ea5d1dea8>", line 1, in <module>
from pybedtools import BedTool
File "/Users/michaelsmith/anaconda2/lib/python2.7/site-packages/pybedtools/__init__.py", line 9, in <module>
from . import scripts
ImportError: cannot import name scripts
I just downloaded Anaconda and there doesn't seem to be a reason as to why this happens. What is the typical protocol for resolving bugs like this?
UPDATE:
So within my pybedtools folder there is a scripts folder (which is presumably the module we're trying to import). I changed both the command within __init__.py to:
from . import scripts2
and changed the name of the folder to scripts2 as well. However, I still get the error as such:
ImportError: cannot import name scripts2
So I must be doing something wrong here, which module should I be renaming exactly? Sorry if this is a silly question, I am quite new to python.
This is caused because Anaconda has a module named scripts and therefore your import is "shadowed" by that module. You can double check that when you call import scripts in a new notebook it works even if you have never defined such a module. A very good explanation of import traps can be found here:
http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html
A workaround would be to rename the script module of pybedtools to something else and also change all the imports to the new name.

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

Failure to import module in 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.

Categories

Resources