using Tensorflow with Anaconda and PyCharm on Windows - python

I am using Anaconda 4.3.1 64 Bit on Windows 10 64 Bit. I have successfully installed Tensorflow (CPU) according to this. The test program runs in the command line, but not in PyCharm. The problem is that it works only with activate tensorflow.
I followed the steps from Pycharm anaconda import tensor flow library issue ("You need to do these following steps:"). It seems now that tensorflow is loaded, but I get a new error:
ImportError:
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try git clean -xdf (removes all
files not under version control). Otherwise reinstall numpy.
How can I use TensorFlow in PyCharm? (BTW: Tensorflow works with PyCharm on my Fedora VM.)
Edit:
Can I use TensorFlow with Jupyter? When running "Jupyter (tensorflow)" from the Anaconda menu, a console window opens and immediately closes.

When PyCharm is open:
if you navigate to preferences: Project: Project Interpreter. You can there either create your own virtualenv (and then manually install the required packages) or make sure you have selected the anaconda python interpreter for your project.

If following official instructions, you have created a virtual environment called tensorflow. The environment is located in Anaconda3\envs\tensorflow directory, where Anaconda3 is the Anaconda installation directory. You just need to point PyCharm to the python.exe which is located there.
The settings path in PyCharm is something like this:
Settings->Project Interpreter->Add Local->Virtualenv Environment->Existing environment
Point the interpreter to the python.exe in the Anaconda3\envs\tensorflow directory.

I recommend installing babun and creating a virtualenv with virtualenvwrapper there and install on that environment.
After doing this, just choose the python binary in the relevant directory of the virtualenv you created, i.e. ~/.virtualenvs/myenv/bin/python or ~/.virtaulenvs/myenv/usr/bin/python

Before importing anything else, do this
import sys
print(sys.path)
import os
print(os.environ)
print(os.environ['CUDA_VISIBLE_DEVICES'])
from the command line (when TF works) and from PyCharm (when it doesn't). If you see any relevant differences, adjust accordingly (define the environment variables in PyCharm, etc.)

Related

Installation of OpenCV in anaconda not showing up in VS Code in Windows 10

I have a VM running only Anaconda. There are no other available installations of Python. This is all I see when I run "Apps and Features" in Windows 10 and filter for Python (note that it is Python 3.7.4):
When I go to the Conda prompt and enter conda list, I see that I have openCV installed :
but it's with a version of Python 3.7.6:
However when I open VS Code and I set my Python Path to the version in the Anaconda folder: (C:\Users\Me\Anaconda3\Python.exe) and try to import OpenCV, I get the "module not found" error. When I create a new python file and run
help("modules")
I do not see OpenCV in the list below in the terminal even though its installed.
Any idea how this is happening? I just don't see a second installation of Python on my PC, but why am I seeing 3.7.4 and 3.7.6 and is this possibly causing my issue?
Edit: Added a picture of the error I get when trying to run it.
Install anaconda plugin in VSCode and select your conda environment in the bottom left of VSCode window.
If you didn't create a conda environment, the you will probably have installed the package in the base environment.
Edit: Reading the PyPi project, I found the right way to import opencv in python code.
import cv2

Python in VSCode can't find module even changed to a right environment

I'm using VSCode for python programming. The interpreters are from Anaconda (Python 3.7 in the latest version of Anaconda). I've added the Anaconda folder path into the system environment variables:
I've installed packages numpy and numba via conda, and I created an environment which contains those packages:
And the current VSCode is working under that environment:
It works fine in PyCharm by using the same interpreter/environment, but
when I switched to VSCode it always gets me error: ImportError: No module named numba. I don't know how to fix that.
You don’t have to manually add environment in VS Code. Remove all the path you have manually added. Bring up the Extensions view by clicking on the Extensions icon in the Activity Bar on the side of VS Code or the View: Extensions command (⇧⌘X (Windows, Linux Ctrl+Shift+X)).
Search for Anaconda extension. Install and refresh your VS Code. Open your project folder and click on of your .py file. VS Code will search all available Pythons in your system.
On the bottom right you should be able to select a correct environments

Issues installing Cantera on Python

I am trying to use Cantera on python. I downloaded Anaconda and followed the instructions on Cantera's website to install. Their website's tutorial says to write:
import cantera as ct
import numpy as np
When I run it, I get the error:
ModuleNotFoundError: No module named 'cantera'
I tried reinstalling everything.
The error ModuleNotFoundError: No module named 'cantera' implies that you have not succesfully installed the 'cantera' module. You stated you downloaded anaconda then followed the install procedure here?
Can you please show me what the output was for this, if there was an error or admin block then cantera was never installed and that is why you can't import it.
You might not be activating your environment. Activate it first in the command prompt and then try to import the modules.
Without knowing the complete detail, my guess is that when you start a new project with Pycharm, it offers to create a new environment for you (See the image below).
An environment acts as an isolated container of packages and multiple environments can be setup (over simplification). By default, when creating a new environment it will only install the default packages with Anaconda and my guess is that Cantera is not a part of that list.
Now you've got two options:
Specifically install the package in the new environment you created:
Click on File -> Settings -> Project -> Project Interpreter
Click the + button on the right hand side of the package list and then look for the package you want to install. It should be installed to your specified environment.
Change the environment to the default anaconda env:
Click on File -> Settings -> Project -> Project Interpreter
In the Project Interpreter drop down, select the default anaconda interpreter. You can look at the packages installed in that env and find if your specific package is there or not. If the package is still not there, you could go ahead and install it using the previous method.
I have gone through a large amount of pain to eventually conclude that:
Cantera 2.4 will work only with Python 2.7, and a new Python 2.7 environment within Anaconda can be used to install it, though I didn't find this written clearly:Cantera Release Notes
To install and run Cantera 2.4:
Create and activate a Python 2.7 environment: Anaconda Documentation
Run the command: conda install -c cantera cantera (first iteration may fail to solve) - Anaconda Documentation
View your Python 2 environment packages in in Anaconda and you will see it listed.

"ImportError: DLL load failed" in PyCharm w/ conda: how to automatically import conda's PATH?

I have installed python via conda, created a new environment, installed PyCharm and configured it to use this environment.
When I start a python console in PyCharm, I am greeted with a
C:\Users\9973879\AppData\Local\Continuum\miniconda3\envs\foo\lib\site-packages\IPython\core\history.py:226: UserWarning: IPython History requires SQLite, your history will not be saved
Indeed if I try to import sqlite3, I get a
ImportError: DLL load failed: The specified module could not be found.
However Project Interpreters > Interpreter Paths does list C:\Users\9973879\AppData\Local\Continuum\miniconda3\envs\foo\{DLLs, Lib, site-packages}. Also, importing this package from a python interpreter launched from the command line — in that very same virtual environment — succeeds.
EDIT
It seems the problem comes from the PATH environment variable. When calling conda activate foo, conda adds a bunch of folders to the PATH, containing various libraries (in particular sqlite3.dll).
When pointing to an existing environment (or creating a new one), PyCharm does automatically add folders to the PYTHONPATH as pointed out before, but does not seem to add anything to the PATH.
I tested this assumption by running PyCharm from the command line after calling conda activate foo, and it works. Of course, having to launch PyCharm from a specific conda environment is not great as I am stuck with this specific environment.
How could PyCharm be configured to automatically update the environment as conda activate does? Shouldn't it do it straight out of the box?
I fixed the issue by adding DLL to my < path_to_environment >/DLLs location
The issue seems to be solved by upgrading to the latest 4.6.7 version of conda. The relevant line from the release notes might be
revert conda prepending to PATH in cli main file on windows (#8307)
I am not sure why it affected PyCharm and not plain python in my case.

No module named 'numpy' in pycharm

I am quite new to python and pycharm.
I installed python 3.6 through anaconda, and I can see many packages including numpy are installed as I can see them in cmd (im using windows) by typing 'conda list'. Also, it works if i type 'import numpy' in python through window command prompt.
However, if I open pycharm and run "import numpy" there, it gives me 'No module named 'numpy' in pycharm'. May I know whats wrong with my setting? I guess it must be some problem with my interpreter setting.
I think my python is installed in C:\Users\AAA\Anaconda3\python.exe
I checked in pycharm, project interpreter is "C:\Users\AAA\PycharmProjects\untitled\venv\Scripts\python.exe"
Should I change it to the one under anaconda3 folder?
What is venv folder under "pycharmprejcts"? Is it a virtual environment? It shows (see the attached screenshot) the base interpreter is the one under anaconda? Should I choose to inherit global site-packages?
You should select Conda environment in Pycharm, not create a new, blank Virtualenv
Or at the very least - System interpreter, then find the Python executable for Anaconda
What is venv folder under "pycharmprejcts"? Is it a virtual environment?
Yes, it allows your project to be more portable - you define the minimum set of dependencies for your code rather than rely on everything installed only locally on your machine
You have a virtual environment, this helps keep the dependencies separate between projects. You can set your project interpreter to C:\Users\AAA\Anaconda3\python.exe or install the dependency in your venv, in pycharm you would press alt+enter on the import and press install.

Categories