iPython: 'no module named' ImportError - python

Windows: I have the Python package CVXOPT installed on my computer for the regular Python distribution, though not specifically with Anaconda, so it imports fine when I'm doing text editor/cmd python scripting. I tried installing CVXOPT with Anaconda, but that didn't work so I'm having to import the library directly when working with iPython.
My directory structure looks like:
C:
--Python27
----Lib
------site-packages
--------cvxopt
----------__init__.py
----------.....
The error occurs when I run this code in an iPython notebook:
import sys
sys.path.append('C:\Python27\Lib\site-packages\cvxopt')
import cvxopt
The error:
ImportError: No module named cvxopt
How can I fix this? Perhaps I'm appending the path incorrectly?

You're defining a path a bit too deep in your file tree. You need to add to sys.path the folder just before your module:
import sys
sys.path.append('C:\Python27\Lib\site-packages')
import cvxopt
Here, cvxopt can be found in the site-packages folder. If you add the cvxopt folder in the sys path, it'll search a module of that name in the folder itself and will not checked the base folder.

Import the path which contains the cvxopt package.
import sys
sys.path.append('C:\Python27\Lib\site-packages')
import cvxopt

Related

package imported from pip installed one instead of local file?

I have the following files:
projx
proj1
__init__.py
mod1.py
tests
test_mod1.py
The package proj1 is already installed on my development machine using pip install. And test_mod1.py has the following import
from proj1.mod1 import ....
However, when running python proj1\tests\test_mod1.py in directory projx, it still import the package from the old package of proj1.mod1 installed by pip install instead of the new one from .\proj1\mod1.py. Shouldn't python use the later one because the current directory is the first item in sys.path?
I created and active venv. However, it got the following error when executing the test file.
Traceback (most recent call last):
File ".\tests\test_......py", line 4, in <module>
from proj1.mod1 import *
ModuleNotFoundError: No module named 'proj1'
I tried to use relative path.
from ..proj1 import *
But It got error of:
ImportError: attempted relative import with no known parent package
You can install multiple version of the same package! Using: pip install packageName will download the newest version, using: pip install packageName==1.0.0 will let you choose older versions aswell.
To import the newest: import packageName.
To import a specified: import packageName_1.0.0
However I think you are right, CWD should always be priority.
I think if you were to try this: from .. import proj1, because parent directories will not be searched for packages.
To demonstrate:
me#myPc /parentDir % ls
childDir
me#myPc /parentDir % cat childDir/cwd.py
import os
print(os.getcwd())
me#myPc /parentDir % python3 childDir/cwd.py
/parentDir
Even though your CWD when telling python to execute the script was projx, the CWD of your script will be it's location: tests. So using from .. import proj1 would tell your script to look for proj1 in it's parent directory and import it.

python module not found error no module named

I have a few seperate pythone file and I am using them to import another py file. Modules that trying to import them are in seperate folder I code sample is below
from tez.library.image_crop import ImageCrop
from tez.library.image_process import ImageProcess
from tez.library.image_features import ImageFeatures
from tez.const.application_const import ApplicationConst
from tez.library.file_operation import FileOperation
this code is in where I want to start the py file using commond line as "python samples1.py" and thrown an error as below
Traceback (most recent call last): File "samples1.py", line 1, in
from tez.library.image_crop import ImageCrop ModuleNotFoundError: No module named 'tez'
folder structure :
.tez
-- library
---- image_crop.py
---- image_process.py
---- image_features.py
--src
---- samples1.py
Python version : 3.8
Pip : 20.0.2
Windows 10 Pro 1909
If you are building a package called tez (and since you tried to import it I think you are). Then everything with tez needs to refer to itself locally. All the files in the tez package need to refer to each other with the "." and ".." imports.
In samples1.py:
from ..library.image_crop import <something>
EDIT:
It sounds like you are misunderstanding how python imports things. When you run "import X" in a python script, then python looks for a package named X under sys.path. You can append to sys.path at the top of your script if you have a custom package to look for.
import sys
sys.path.append(<directory of tez>)
import tez
However, it is strongly recommended that you should not be importing from a file that is under the directory structure of the package name. If "examples" is a directory of examples that use the package "tez" then "examples" should be located outside the package "tez". If "examples" is inside the package "tez", then "examples" should be doing local imports "with-in" the package.
Getting a handle on package use can be tricky.
sample.py can't see above of src folder, but you can tell Python to do this.:
import sys
import os
tez = os.path.dirname(os.path.dirname(__file__))
# __file__ is path of our file (samples.py)
# dirname of __file__ is "src" in our state
# dirname of "src" is "tez" in our state
sys.path.append(tez) # append tez to sys.path, python will look at here when you try import something
import library.image_crop # dont write "tez"
But this is not a very good design I think.

No module named 'lstm_object_detection' in tensorflow /model/research/lstm_object_detection

I gitcloned lstm from tensorflow research models. Running seq_dataset_builder.py from inputs file the following error appears
No module named 'lstm_object_detection'.
The line making the error is
from lstm_object_detection.inputs import tf_sequence_example_decoder
lstm_object_detection/inputs is the directory of the python script file.
My tensorflow version is 1.7.0 and the file is run on python3.
I also can't import tensorflow.google.
Thanks in advance.
It might be that Python doesn't find the lstm_object_detection in your path
import os
import sys
sys.path.append(os.getcwd()) # add current directory to your path
from lstm_object_detection.inputs import tf_sequence_example_decoder
To check the paths where Python looks for modules use:
import sys
for path in sys.path:
print(path)

Python import error: No module named toolkit.rd

I have installed a third party library according to its instruction using
python setup.py install
the library is now located in C:\Python27\Lib\site-packages.
However when calling
from myLib.toolkit.rd import myFunction
I get an error:
ImportError: No module named toolkit.rd
The folder structure is myLib->toolkit->rd.py. myLib and toolkit folder contain an __init__.py. I also added the path to python path environment variable but that did not help either.
So I'm kind of lost why this isn't working?
EDIT:
from myLib.geometry import distance_to_point
where geometry.py contains function distance_to_point results in following error:
ImportError: No module named geometry
So something with this library is just wrong. I have a lot of other libs installed and they all work with no issue. Note that I did restart PC. Just to make sure...
Maybe you have myLib/__init__.py not empty with an __all__ definition that not expose toolkit submodule.

How to import a module from a directory on level above the current script

For my Python application, I have the following directories structure:
\myapp
\myapp\utils\
\myapp\utils\GChartWrapper\
\myapp\model\
\myapp\view\
\myapp\controller\
One of my class in \myapp\view\ must import a class called GChartWrapper. However, I am getting an import error...
myview.py
from myapp.utils.GChartWrapper import *
Here is the error:
<type 'exceptions.ImportError'>: No module named GChartWrapper.GChart
args = ('No module named GChartWrapper.GChart',)
message = 'No module named GChartWrapper.GChart'
What am I doing wrong? I really have a hard time to import modules/classes in Python...
The __init__.py file of the GChartWrapper package expects the GChartWrapper package on PYTHONPATH. You can tell by the first line:
from GChartWrapper.GChart import *
Is it necessary to have the GChartWrapper included package in your package directory structure?
If so, then one thing you could do is adding the path where the package resides to sys.path at run time. I take it myview.py is in the myapp\view directory? Then you could do this before importing GChartWrapper:
import sys
import os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'utils')))
If it is not necessary to have it in your directory structure, it could be easier to have it installed at the conventional location. You can do that by running the setup.py script that's included in the GChartWrapper source distribution.
You don't import modules and packages from arbritary paths. Instead, in python you use packages and absolute imports. That'll avoid all future problems.
Example:
create the following files:
MyApp\myapp\__init__.py
MyApp\myapp\utils\__init__.py
MyApp\myapp\utils\charts.py
MyApp\myapp\model\__init__.py
MyApp\myapp\view\__init__.py
MyApp\myapp\controller\__init__.py
MyApp\run.py
MyApp\setup.py
MyApp\README
The files should be empty except for those:
MyApp\myapp\utils\charts.py:
class GChartWrapper(object):
def __init__(self):
print "DEBUG: An instance of GChartWrapper is being created!"
MyApp\myapp\view\__init__.py:
from myapp.utils.charts import GChartWrapper
def start():
c = GChartWrapper() # creating instance of the class
MyApp\run.py:
from myapp.view import start
start()
That's all! When you run your entry point (run.py) it calls a function on the view, and that creates an instance of the GChartWrapper class. Using this structure you can import anything anywhere and use it.
To complement, in MyApp\setup.py you write an installation program for the MyApp\myapp package. Use distutils to write it:
from distutils.core import setup
setup(name='MyApp',
version='1.0',
description='My Beautiful Application',
author='Martin',
author_email='martin#xxxxxxx.com',
url='http://stackoverflow.com/questions/1003843/',
packages=['myapp'],
scripts=['run.py']
)
That is enough. Now when people download the MyApp folder, they can just install it using setup.py and run it using run.py. Distutils can generate packages in a number of formats including windows installable .EXE
It's the standard way of distributing python packages/applications.
You can change the path where python looks for files.
At the top of your source file, add:
import sys
sys.path.append("..")
Or alternatively change the environment variable:
export PYTHONPATH=..
Or starting in python 2.5 (again assuming myview is in myapp\view:
from __future__ import absolute_import
from ..utils.GChartWrapper import *
See: http://docs.python.org/whatsnew/2.5.html#pep-328-absolute-and-relative-imports
GChartWrapper is also available from PyPI so you can use easy_install or pip to install the module:
sudo pip install GChartWrapper==0.9
It will then be automatically added to your PYTHONPATH and then you can remove it from your /myapp/utils directory. If you can't use sudo, look at using virtualenv (and virtualenvwrapper).

Categories