Anaconda environment not using packages saved in the environment - python

I created an environment using the command conda create -n myenv python=3.9. I entered the environment using conda activate myenv, and did not install any packages. When I try to run a file using python3 my_file which imports NumPy without a problem. This is because it accesses packages stored at a different location: c:\users\zador\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-packages. I figured this out by uninstalling at that location, and indeed the code did not run.
Additionally, I tried to get install NumPy in my new environment so I ran both conda install numpy and pip3 install numpy, and after both installations I got the error ModuleNotFoundError: No module named 'numpy'.
I don't get what's going on. I am in my environment but packages are being accessed at a different location.
Some additional information that may or may not be helpful:
After the command where python, I get the output:
C:\Users\zador\anaconda3\envs\myenv\python.exe
C:\Users\zador\AppData\Local\Microsoft\WindowsApps\python.exe`
but after the command where python3, I get the output:
C:\Users\zador\AppData\Local\Microsoft\WindowsApps\python3.exe
Any help would be very much appreciated! When in a conda environment I want to access packages installed in that environment.

Related

How to install NetCDF4 module in Spyder?

I don't know why this is causing me so much headache.
I know how to use pip, and have the latest version, but still when running a script in Spyder that requires netCDF4 (import netCDF4) Spyder always returns:
import netCDF4
ModuleNotFoundError: No module named 'netCDF4'"
I opened cmd, pip install netCDF4, confirmed it installed OK. Shouldn't this be enough?
I manually copied a downloaded version of netCDF4, moved it to my Python site packages, and then in Spyder manually went into PYTHONPath Manager -> Add Path --> Added the folder location 'netCDF4-1.6.0'. I thought this would definitely be enough?
Then INSIDE Spyder's python console, bot-right of screen, I tried 'pip install netCDF4' and it returns:
Note: you may need to restart the kernel to use updated packages.
C:\...\Spyder\Python\python.exe: No module named pip
So is my issue #3? Spyder's version of Python doesn't have pip and doesn't link to the netCDF4 module? I thought my step #2 would resolve this?
Any help on how to install modules in Spyder would be appreciated, thank you!
Download Miniconda and install all the packages you want in a new environment. For example,
conda create -n myenv -c conda-forge python=3.9 netcdf4 [others ...]
Then follow the Spyder documentation for adding a conda environment.
Credit to Michael Delgado (above)

Issue with installing python modules

I am pretty new to python. Just been working through some online tutorials on udemy. I seem to have an issue with pip installing modules.
I've tried reinstalling them.
Upgrading my python version.
In VS I always just get module not found.
If I do it in the cmd prompt this is what I get below.
You are currently working on the base environment of your computer. For safety, you can first create a new virtual environment with
python3 -m venv -n new_env
So that you won't corrupt any default installations. Then, activate it with
source new_env/bin/activate
And update the pip and setuptools with
pip3 install --upgrade pip
pip3 install --upgrade setuptools
Finally, install numpy via
pip3 install numpy
However, I would recommend using Anaconda to build your virtual environment. When you install Anaconda and make sure it is included in the path of your terminal, all you need to type is
conda create -n new_env python=3.7 numpy
and it will automatically build the wheel for numpy. Here, "new_env" is just an example for a virtual environment name, and Python version 3.7 is also an example.
You can then, activate this conda environment by
conda activate new_env
To use this virtual environment, which you built either with "venv" or "conda", you should locate and activate this environment from the project interpreter settings in VS .
Finally, I would also recommend considering Pycharm IDE which can also help you with creating a virtual environment and installing packages in it.
It seems that you already have the packages installed. Using VS, please, be sure that you selected the correct Python interpreter (https://code.visualstudio.com/docs/python/environments)

Creating new Conda environment messes with old environments?

I have 2 different projects with different dependencies. One requires Tensorflow1.15 and another needs 1.14. I first created an environment env1 and pip installed Tf1.14, ran my code, all went well. Then I created a new environment env2 and pip installed Tf1.15, during which I could see it was uninstalling Tf1.14, I assumed it knew what it was doing. However now when I run my code in env1 it throws errors because tf1.14 is removed and env1 also tries to use tf1.15!
What am I doing wrong? I thought we use Conda to create completely separate environments for specifically this kind of situation but I'm confused.
You should not have used pip to install the packages. If you have Anaconda you should use conda to install them. conda install numpy will install the numpy module.
You need
conda install your-module

python can't find conda packages but can find pip packages

I'm sure this might be a common problem but I've scoured the StackOverflow and read through all the recommended posts that StackOverflow gave me before posting. However, I've installed tensorflow on conda but not on pip. When I run Python, I can't run import tensorflow since it gives me ModuleNotFoundError: No module named 'tensorflow'. This leads me to believe that conda installs the packages to a different place than pip, but I'm not sure how to make them install to the same place. I recall a long time ago, the problem being related to usr/bin and usr/local/bin or something, but I don't remember how that goes or the solution.
Any links / tips would be appreciated!
Please follow the below steps:
The below command will list all the environments in the system and their path
conda env list
Please activate the corresponding environment using the below command
conda activate <env_name>
You can list the pip/conda packages that you have installed in the activated environment using the below command
pip freeze
conda list
If tensorflow is not listing, please install tensorflow again using the below command
pip install tensorflow
If still you are facing issue check the PATH variable in control panel and set python path.

ImportError: No module named 'keras_contrib'

I am trying to import Keras lib code to execute CRF using the import command below but an error raises as titled. Please share the solution for this.
The command used to execute is
from keras_contrib.layers import CRF
Traceback (most recent call last):
File "", line 1, in
from keras_contrib.layers import CRF
ImportError: No module named 'keras_contrib'
A simple
(sudo) pip install git+https://www.github.com/keras-team/keras-contrib.git
as mentioned in the installation instructions did the trick for me.
This error means that Python is unable to find the module in one of the directories defined by Python path. The module is either not installed or is installed in another directory.
If not installed, then see https://github.com/keras-team/keras-contrib for installation instructions.
If installed but not found, you will most likely need to add the directory where it is installed to your Python path. You can find out what your current Python path is by inspecting the variable sys.path (such as python -c 'import sys; print sys.path'). You may need to add another directory to your path by setting the environment variable PYTHONPATH before running your script, but there are other options. See for example PYTHONPATH vs. sys.path for some insight.
After struggling for a while, I was so willing to make myself clear of this issue, so I searched for a while, and just figured out and tested.
When you create a new conda env by specifying the python version, it will use the conda_root_python version. And if you didn't install the pip package, and try to use pip under your created conda env, it will only run the conda_root_pip and install the package in the root site_packages.
I know three ways to install python packages only in your created conda env.
For a better explanation, we create a conda env with same python version of conda root environment.
conda create -n myenv python
I. One of the officials advise, install package with conda command for specified conda environment,
conda install -n myenv tensorflow
II. Another one of official advise, get into your specified environment and run conda install
source activate myenv
conda install tensorflow
in above two ways you don't need to install extra packages like pip and other pip related packages.
III. For people who really want to pip, just because get used of that.
install pip package(just as above two ways did).
conda install -n myenv pip
or
source active myenv
conda install pip
then comes the pip install when you are in your environment
pip install tensorflow
--------new edit above 15.April.2018--------------
Just to make it more clear.
If you are working under anaconda environment, you should also install all the modules and IDE you need in that environment.
Here I just put one example of anaconda env flows:
conda create --name=my_conda_env python=2.7 #create an environment
activate my_conda_env #get into that env
pip install numpy #install packages you need
...
pip install keras_contrib
pip install spyder #install IDE
Getting Started with conda
---------
Try install in root
activate root
pip install keras_conrib
go back to your tensorflow
start your spyder and try again
Maybe this is your issue
Module installed on Conda, but gives error on importing in Spyder (Python IDE)
----------------- above new answer
It seems you are under conda environment, env-name is "tensorflow", so try to start python and try import again. To make it clear
make sure you have (tensorflow) in front of C:\Users>
type python to start python
import keras_contrib to see if you have keras_contrib in anaconda env (tensorflow) due your comment, it should be
from keras_conrib.layers import CRF (crf or CRF? just try)
If you installed keras_contrib in env "tensorflow", should also start python and do your jobs in the same env, for a new env, you have to install it again.
Here is something for newbie just like me after playing with python for a while and still not familiar with anaconda, I hope you didn't come up with that. As follows:
I used to think in my anaconda env is already in python(actually not yet), so I just type
from keras_contrib.layers import CRF when I saw (tensorflow)C:/Users> which is actually wrong
The right way as described up is get into python(step 2.) or ipython or jupyter just for test if you get the package.
--------------------- below is old answer
I think you confused keras with keras_contrib.
They are two different modules.
try pip install keras_contrib or use other ways to install keras_contrib.
If you are trying to install the tensorflow-keras version or even the keras version using git cloning and setup.py installing and getting the above error then you might want to add the path of the keras-contrib folder to system path using -
import sys
sys.path.append('<remaining_path>/keras_contrib')
Simply run:
conda install git+https://www.github.com/keras-team/keras-contrib.git
If you use Tensorflow, you may get over this error by replacing
from keras_contrib.layers import CRF
with
from tensorflow_addons.layers import CRF
If you're still interested in Keras itself, use the following line in Jupyter
!pip install git+https://www.github.com/keras-team/keras-contrib.git
Just note that you may face other errors while using seperate Keras, so I suggest that you use Keras that is supported in Tensorflow.

Categories