Discussion:
[conda] Why does `import tensorflow` not raise error if `Package(s) is missing from the environment`?
b***@me.com
2018-04-19 04:24:31 UTC
Permalink
I have a short convenience script that reports the versions of certain
packages in the current environment. It includes a series of code snippets
like this:

from platform import python_version


print('python: %s' % python_version())


if python_version() < '3.6':
import_exception = ImportError
else:
import_exception = ModuleNotFoundError


try:
import tensorflow
except import_exception:
print('tensorflow: not installed')
else:
print('tensorflow: %s' % tensorflow.__version__)

...


Typical output looks like this:

python: 3.6.5
tensorflow: 1.5.0

...


I recently created a new environment with Python 3.4 and now the code
doesn't work. On investigation `import tensorflow` does not raise an error
in this environment even though tensorflow is not installed. To prove it,
see the following console output:

(py34) BillsMacBookPro:~ billtubbs$ python
Python 3.4.5 | packaged by conda-forge | (default, Sep 8 2016, 14:36:28)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
import tensorflow
tensorflow.__version__
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__version__'
dir(tensorflow)
['__doc__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
exit()
(py34) BillsMacBookPro:~ billtubbs$ conda update tensorflow


PackageNotInstalledError: Package is not installed in prefix.
prefix: /Users/billtubbs/anaconda/envs/py34
package name: tensorflow





Can anyone explain this? I expect
import tensorflow
to raise an error if `Package is not installed`. If it doesn't raise an
error, I expect tensorflow to import correctly. Here it seems to have
loaded an empty module called `tensorflow`
--
You received this message because you are subscribed to the Google Groups "conda - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to conda+***@continuum.io.
To post to this group, send email to ***@continuum.io.
Visit this group at https://groups.google.com/a/continuum.io/group/conda/.
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/conda/bb2a6e6e-3d43-482f-a799-e820043ef066%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.
Thomas Holder
2018-04-19 09:32:31 UTC
Permalink
Could be a tensorflow.py file in your current directory. After the import, print tensorflow or tensorflow.__file__ will tell you were Python found the module.
Post by b***@me.com
Post by b***@me.com
import tensorflow
tensorflow.__file__
'tensorflow.py'
Thomas
Post by b***@me.com
from platform import python_version
print('python: %s' % python_version())
import_exception = ImportError
import_exception = ModuleNotFoundError
import tensorflow
print('tensorflow: not installed')
print('tensorflow: %s' % tensorflow.__version__)
...
python: 3.6.5
tensorflow: 1.5.0
...
(py34) BillsMacBookPro:~ billtubbs$ python
Python 3.4.5 | packaged by conda-forge | (default, Sep 8 2016, 14:36:28)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Post by b***@me.com
Post by b***@me.com
import tensorflow
tensorflow.__version__
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__version__'
Post by b***@me.com
Post by b***@me.com
dir(tensorflow)
['__doc__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
Post by b***@me.com
Post by b***@me.com
exit()
(py34) BillsMacBookPro:~ billtubbs$ conda update tensorflow
PackageNotInstalledError: Package is not installed in prefix.
prefix: /Users/billtubbs/anaconda/envs/py34
package name: tensorflow
Can anyone explain this? I expect
import tensorflow
to raise an error if `Package is not installed`. If it doesn't raise an error, I expect tensorflow to import correctly. Here it seems to have loaded an empty module called `tensorflow`
--
Thomas Holder
PyMOL Principal Developer
Schrödinger, Inc.
--
You received this message because you are subscribed to the Google Groups "conda - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to conda+***@continuum.io.
To post to this group, send email to ***@continuum.io.
Visit this group at https://groups.google.com/a/continuum.io/group/conda/.
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/conda/6541AFC3-5672-428D-AABD-B6B5A38D60E8%40schrodinger.com.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.
b***@me.com
2018-04-19 15:20:24 UTC
Permalink
Thanks Thomas. You nailed it.

tensorflow.__file__ does not work but there was a folder called
'tensorflow' with some files in it. Python was looking for a module in it
but I guess even though there was no __init__.py file it set up tensorflow
as a namespace or something.

thanks again.
Bill.
Post by Thomas Holder
Could be a tensorflow.py file in your current directory. After the import,
print tensorflow or tensorflow.__file__ will tell you were Python found the
module.
Post by b***@me.com
Post by b***@me.com
import tensorflow
tensorflow.__file__
'tensorflow.py'
Thomas
Post by b***@me.com
I have a short convenience script that reports the versions of certain
packages in the current environment. It includes a series of code snippets
Post by b***@me.com
from platform import python_version
print('python: %s' % python_version())
import_exception = ImportError
import_exception = ModuleNotFoundError
import tensorflow
print('tensorflow: not installed')
print('tensorflow: %s' % tensorflow.__version__)
...
python: 3.6.5
tensorflow: 1.5.0
...
I recently created a new environment with Python 3.4 and now the code
doesn't work. On investigation `import tensorflow` does not raise an error
in this environment even though tensorflow is not installed. To prove it,
Post by b***@me.com
(py34) BillsMacBookPro:~ billtubbs$ python
Python 3.4.5 | packaged by conda-forge | (default, Sep 8 2016,
14:36:28)
Post by b***@me.com
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Post by b***@me.com
Post by b***@me.com
import tensorflow
tensorflow.__version__
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__version__'
Post by b***@me.com
Post by b***@me.com
dir(tensorflow)
['__doc__', '__loader__', '__name__', '__package__', '__path__',
'__spec__']
Post by b***@me.com
Post by b***@me.com
Post by b***@me.com
exit()
(py34) BillsMacBookPro:~ billtubbs$ conda update tensorflow
PackageNotInstalledError: Package is not installed in prefix.
prefix: /Users/billtubbs/anaconda/envs/py34
package name: tensorflow
Can anyone explain this? I expect
import tensorflow
to raise an error if `Package is not installed`. If it doesn't raise
an error, I expect tensorflow to import correctly. Here it seems to have
loaded an empty module called `tensorflow`
--
Thomas Holder
PyMOL Principal Developer
Schrödinger, Inc.
--
You received this message because you are subscribed to the Google Groups "conda - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to conda+***@continuum.io.
To post to this group, send email to ***@continuum.io.
Visit this group at https://groups.google.com/a/continuum.io/group/conda/.
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/conda/a3c21a66-5445-4e32-844e-031ece10df1b%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.
b***@me.com
2018-04-19 15:30:58 UTC
Permalink
Thanks Thomas. You nailed it.

tensorflow.__file__ does not work but there was a folder called
'tensorflow' with some files in it. It was an accidental module! (I found
quite a few others actually).

Mental note: Don't name folders with Python module names...

thanks again.
Bill.
Post by Thomas Holder
Could be a tensorflow.py file in your current directory. After the import,
print tensorflow or tensorflow.__file__ will tell you were Python found the
module.
Post by b***@me.com
Post by b***@me.com
import tensorflow
tensorflow.__file__
'tensorflow.py'
Thomas
Post by b***@me.com
I have a short convenience script that reports the versions of certain
packages in the current environment. It includes a series of code snippets
Post by b***@me.com
from platform import python_version
print('python: %s' % python_version())
import_exception = ImportError
import_exception = ModuleNotFoundError
import tensorflow
print('tensorflow: not installed')
print('tensorflow: %s' % tensorflow.__version__)
...
python: 3.6.5
tensorflow: 1.5.0
...
I recently created a new environment with Python 3.4 and now the code
doesn't work. On investigation `import tensorflow` does not raise an error
in this environment even though tensorflow is not installed. To prove it,
Post by b***@me.com
(py34) BillsMacBookPro:~ billtubbs$ python
Python 3.4.5 | packaged by conda-forge | (default, Sep 8 2016,
14:36:28)
Post by b***@me.com
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Post by b***@me.com
Post by b***@me.com
import tensorflow
tensorflow.__version__
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute '__version__'
Post by b***@me.com
Post by b***@me.com
dir(tensorflow)
['__doc__', '__loader__', '__name__', '__package__', '__path__',
'__spec__']
Post by b***@me.com
Post by b***@me.com
Post by b***@me.com
exit()
(py34) BillsMacBookPro:~ billtubbs$ conda update tensorflow
PackageNotInstalledError: Package is not installed in prefix.
prefix: /Users/billtubbs/anaconda/envs/py34
package name: tensorflow
Can anyone explain this? I expect
import tensorflow
to raise an error if `Package is not installed`. If it doesn't raise
an error, I expect tensorflow to import correctly. Here it seems to have
loaded an empty module called `tensorflow`
--
Thomas Holder
PyMOL Principal Developer
Schrödinger, Inc.
--
You received this message because you are subscribed to the Google Groups "conda - Public" group.
To unsubscribe from this group and stop receiving emails from it, send an email to conda+***@continuum.io.
To post to this group, send email to ***@continuum.io.
Visit this group at https://groups.google.com/a/continuum.io/group/conda/.
To view this discussion on the web visit https://groups.google.com/a/continuum.io/d/msgid/conda/f9abcd29-7859-484f-b44b-f8fb7925a14c%40continuum.io.
For more options, visit https://groups.google.com/a/continuum.io/d/optout.
Loading...