I am writing the following code in Jupyter notebook.
import tensorflow as tf
g = tf.Graph()
And I encounter the following error while executing this simple code.
AttributeError: module 'tensorflow' has no attribute 'Graph'
If I execute the same from the console. It works. Any idea what's happening here.
More information:(test is my virtualenv)
ipython, jupyter, python version:
(test) xingzhou#xingzhou008:~/tensorflow$ which ipython3
/home/xingzhou/tensorflow/test/bin/ipython3
(test) xingzhou#xingzhou008:~/tensorflow$ which jupyter
/home/xingzhou/tensorflow/test/bin/jupyter
(test) xingzhou#xingzhou008:~/tensorflow$ which python3
/home/xingzhou/tensorflow/test/bin/python3
Execute from Console:
(test) xingzhou#xingzhou008:~/tensorflow$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> g = tf.Graph()
>>> type(g)
<class 'tensorflow.python.framework.ops.Graph'>
>>>
Make sure you haven't called any of your python files as 'tensorflow.py'
I resolved this issue by restarting Jupyter Notebook kernel. It's weird.
A simple solution worked for me.
From anaconda navigator first go to environments and create a tensorflow environment like this image
I named it tensorflow. Then from the right select all from the drop down menu and search for tensorflow.
Select it and hit apply. It will download some additional packages along with tensorflow. When you are done, select tensorflow from home and you should be good to go. Here it works.
Check if TensorFlow is installed in Anaconda/Environments. If not, just search and install. Then try to run g=tf.Graph() again. It worked for me.
Related
I have been using vs code and its data science module for a long time to create and edit python scripts via SSH. Recently I tried to open a jupyter notebook with it but I am having issues with the python interpreter
The python interpreter selected is the one from the base environment from miniconda
However, when I try to import a module that is installed on conda base I am getting
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-094d2d97d2ef> in <module>
1 # imports
2 import pandas as pd
----> 3 import matplotlib.pyplot as plt
ModuleNotFoundError: No module named 'matplotlib'
I am sure that matplotlib is installed on the base environment. If, on a terminal window, on VS code itself, I
open the same python interpreter (/home/user/miniconda3/bin/python)
call the same import (import matplotlib.pyplot as plt)
I get no error:
(base) user#Brightcore-testsrv:~$ which python
/home/user/miniconda3/bin/python
(base) user#Brightcore-testsrv:~$ /home/user/miniconda3/bin/python
Python 3.7.3 (default, Mar 27 2019, 22:11:17)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
>>>
Your VS Code Jupyter notebook may be running with a different interpreter as its kernel. You can execute this code to check:
import sys
print(sys.executable)
If the result of running the code above is not /home/user/miniconda3/bin/python, you can use the kernel selector to switch kernels to your desired environment.
After installing pandas am able to import in cmd as below :
C:\Users\me\Desktop\Django_Project>python
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas
>>>
But when am importing pandas in Spyder in IPython 6.5.0 console I get below error:
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.
IPython 6.5.0 -- An enhanced Interactive Python.
c:\program files (x86)\python37-32\lib\site-packages\ipykernel\parentpoller.py:116: UserWarning: Parent poll failed. If the frontend dies,
the kernel may be left running. Please let us know
about your system (bitness, Python, etc.) at
ipython-dev#scipy.org
ipython-dev#scipy.org""")
In [1] : import pandas
Traceback (most recent call last):
File "<ipython-input-1-38d4b0363d82>", line 1, in <module>
import pandas
ModuleNotFoundError: No module named 'pandas'
In [2] :
Note: I have installed python in "C:\Users\me\AppData\Local\Programs\Python\Python37\" path and environment variables is set as "C:\Users\me\AppData\Local\Programs\Python\Python37;"
And I installed pandas using PIP
Please suggest the solution to resolve this issue.I have tried reinstalling the pandas package almost 6-7 times.
I think you should open anaconda command prompt by searching it in windows search bar . Now write there "conda install -c anaconda pandas" and try again to run the program
I have a problem trying to import cv2 in python3 when I run it from my home folder...
luis#luis-Ubuntu:~$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so: undefined symbol: PyCObject_Type
>>>
but when I run python3 inside /usr/lib/python3/dist-packages or /usr/local/lib/python3.5/dist-packages path it works fine...
luis#luis-Ubuntu:~$ cd /usr/lib/python3/dist-packages
luis#luis-Ubuntu:/usr/lib/python3/dist-packages$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>
I know this is related to include a path on the sys library,
I added the export PYTHONPATH on my ~/.bashrc but didn't solve the problem...
export PYTHONPATH="/usr/local/lib/python3.5/dist-packages:$PYTHONPATH"
I also found that if I insert the path before importing cv2 it works but I need to do this on all scripts or every time I run python3 from a terminal...
luis#luis-Ubuntu:~$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so: undefined symbol: PyCObject_Type
>>> import sys
>>> sys.path.insert(0,'/usr/lib/python3/dist-packages')
>>> import cv2
>>>
but I want to fix this permanently, does anyone know how to solve this...
btw... runs fine on python2...
From the print(sys.path) that you have provided,
...'/opt/ros/kinetic/lib/python2.7/dist-packages'...
I think the problem lies in this item. Although the python3 path also resides in sys.path, the python2.7 path precedes it. Python will catch the one in python2.7 first. When you are running in directly inside /python3/dist-packages, the current directory is placed first, and that precedes python2.7.
I think there are two ways:
Remove python2.7/dist-packages from your PYTHONPATH
Call sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages') before you import cv2. See this question.
The first approach should be the "proper" way but I am not sure how it is deal with if you want to use both python2/3 at the same time. You might be interested in this post.
You can add the following two lines before you import cv2 in your python code. It works for me without changing any source file:
import sys
sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')
Then import cv2:
import cv2
The root of this problem are the ROS commands like source/opt/ros/kinetic/setup.bash in the bashrc file /home/username/.bashrc , which force changes in the Python path. Even if you are not using ROS, the commands are still executed and thus you are directed to find cv2 in /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so in the ROS folder, which is not compatible with Python 3. More discussion on the same issue can be seen at:
After install ROS Kinetic, cannot import OpenCV
. Here I propose another solution which is not mentioned by any answer at that post.
The idea is to run source/opt/ros/kinetic/setup.bash(and any other bash related to ROS) only when using ROS. This way you do not have to modify .bashrc file (or editing the PYTHONPATH like to what you do) every time.
First, remove ROS-related commands like source/opt/ros/kinetic/setup.bash from the basrch file and make sure that you can import cv2 in Python 3.x with no error now.
Then, create a environment and install all the ROS related packages here. By doing so, we will have to activate this ros_environment when running ROS. For creating environment, see
https://conda.io/docs/user-guide/tasks/manage-environments.html#creating-an-environment-with-commands
Next, activate your newly-created environment and follow
https://conda.io/docs/user-guide/tasks/manage-environments.html#macos-and-linux
to create files in the suggested path. Namely,
cd $CONDA_PREFIX
mkdir -p ./etc/conda/activate.d
mkdir -p ./etc/conda/deactivate.d
touch ./etc/conda/activate.d/env_vars.sh
touch ./etc/conda/deactivate.d/env_vars.sh
Edit the ./etc/conda/activate.d/env_vars.sh as follows
source/opt/ros/kinetic/setup.bash
Also add in any other ROS-related bash in the file.
As for ./etc/conda/deactivate.d/env_vars.sh, what I do is exporting the PYTHONPATH back to Python 3. For example, it could be
export PYTHONPATH="/usr/lib/python3/dist-packages"
Note that this is not really deactivating the source/opt/ros/kinetic/setup.bash command. I just found doing this way would direct my PYTHONPATH back to the default python 3, and the correct cv path can be found even after activation and deactivation of the ROS environment .
I am running Ubuntu 16 and have both python 2 and 3. I have downloaded wxpython and it works with the python2 interpreter but not 3. I get
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
>>>
and
Python 3.5.2 (default, Sep 14 2017, 22:51:06)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import wx
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'wx'
What do I need to do to get installed for python 3?
A similar situation arises on Fedora 25, on which I was able to solve this. Your mileage may vary on Ubuntu.
First note that wxPython is available in two major versions, let's call them wx3 and wx4. You can identify your running version through import wx; print(wx.version()). The version string on Fedora 25 reads '3.0.2.0 gtk3 (classic)', i.e. a brand of wx3. On sourceforge these versions are known as 'wxPython' and 'wxPython4', and wxpython.org calls wx4 'phoenix'.
Inspecting the source code of wx3 you will note that wx3's syntax is incompatible with python3. Conversely, wx4 is compatible both with python2.7 and python3.
wx4 doesn't seem to be available on Fedora 25, therefore python3 can't run any wx out-of-the-box. Ubuntu may or may not have the same issue.
The following worked for me to install wx4 in a python3 virtual environment:
pip install -U -f https://extras.wxpython.org/wxPython4/extras/linux/gtk3/fedora-26 wxPython
I presume the answer to your question would be
pip install -U -f https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-16.04 wxPython
In case you need to port a (py2, wx3) application to python3, you would be wise creating an intermediate step: (py2, wx3) -> (py2, wx4) -> (py3, wx4), noting that (py3, wx3) is an impossibility.
To create the (py2, wx4) environment was more cumbersome for me, because the above pip install command fails to find header files when run using pip2.
What ended up working for me was to download the 4.0.0b2 source https://pypi.python.org/packages/bc/6f/f7bb525517557e1c596bf22ef3f242b87afaeab57c9ad460cb94b3b0714e/wxPython-4.0.0b2.tar.gz#md5=2e3716205da8f52d8039095d14534bf7
and follow the build instructions https://github.com/wxWidgets/Phoenix/blob/master/README.rst , from which I only used the build command python build.py dox etg --nodoc sip build .
After building, you need to tell your python2 where to find the wx4 library. I ended up doing that by creating a virtualenv, and creating a symbolic link like so:
/home/user/venv/lib/python2.7/site-packages/wx -> /home/user/downloads/wxPython/wxPython-4.0.0b2/wx
That latter directory cointaining the result of the build.
I have successfully installed Python3 and Pygame using the homebrew methods found here: Installing Pygame on Mac OS X with Python 3
After it was all done, I ran IDLE3, and the Import Pygame command ran successfully.
However now my goal is to get Wing IDE (specifically wing 101 for now) running and have it import pygame as well, but it does not seem to recognize pygame.
2.7.10 (default, Jul 13 2015, 12:05:58)
[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]
Python Type "help", "copyright", "credits" or "license" for more information.
[evaluate untitled-2.py]
Traceback (most recent call last):
Python Shell, prompt 2, line 1
ImportError: No module named pygame
Then when i try to do the Edit < Configure Python < (change the path) method, all I get is this error
Some values are invalid:
Python executable '/usr/local/Cellar/python3/3.4.3_2/IDLE 3.app' is not a file. It should be the name of a Python interpreter that is on
your PATH (such as python, python3.4, python.exe) or the full path to
the Python interpreter you wish to use.
Please correct the values and try again.
I've tried looking everywhere for a solution, I'm pretty new at all of this as the only language i currently know is VB6 (basically caveman talk), but i really just want to get everything running smoothly so I can really get started learning.
Thanks :)
I'm not on Mac right now, so I can't test this, but it looks like it can't find the correct Python interpreter. Fortunately, IDLE can find the correct python interpreter. Open IDLE and use:
import sys
sys.executable
to find the correct python path to use. In your WingIDE settings, use that path.
Basically Running This "import sys; print(sys.executable)" in the IDLE i already had working gave me the correct path to plug into Wing.
Everything runs beautifully now, Big thankyou to everyone who provided input to my problem and solution. Haken Lid & Ben