How to link torch from anaconda to python file? - python

I have got python38,37 and anaconda on my machine. I did install torch via conda:
conda --version
conda 4.8.4
However, my python file can't import torch, running pip install torch gives famous no module nn.wrap found error. I can import torch in jupyter(anaconda distro) though.
I tried fixing nn.wrap bug, but failed:
pip3 install https://download.pytorch.org/whl/cu90/torch-1.1.0-cp38-cp38m-win_amd64.whl
Or
pip install torch===1.6.0 torchvision===0.7.0 -f https://download.pytorch.org/whl/torch_stable.html
Did not work.
I just want to use that torch from anaconda, how to import it instead of searching it in python3.8 dir?

Related

Even after importing Numpy not recognised

My code:
import numpy as np
import tensorflow
Output:
NameError Traceback (most recent call last)
< ipython-input-1-87dbf2ff9b36 > in < module >
----> 1 x = np.random.randint(0,100)
NameError: name 'np' is not defined
What I don't understand is that even though I have already imported the library it is not getting detected and gives me a NameError, Same is the case with a lot of libraries.
I am currently using Jupyter Lab through Anaconda.
=================================>
Got the solution for all the Issues while installing TensorFlow on M1 Macbook
Follow the below link:
https://naturale0.github.io/machine%20learning/setting-up-m1-mac-for-both-tensorflow-and-pytorch#:~:text=Macs%20with%20ARM64%2Dbased%20M1,both%20from%20consumers%20and%20developers.
Thank you #Akella Niranjan for the reference. For the benefit of community providing solution here as written by #Sihyung Park.
Install TensorFlow-macOS for Apple Silicon M1
It is easy to install it with the system python since the installation script is given by Apple. However, my goal was to install it with other python so that I can install additional packages for data science without difficulty.
I mainly followed instructions from here to install tensorflow-macos, and then created ipykernel of it so that I can run this environment any time by switching the kernel inside jupyter notebook.
First install miniforge, which natively supports M1. The installation defaults to directory ~/miniforge3/.
Create virtual environment named tf_macos with conda and install Python 3.8.
conda create -n tf_macos
conda activate tf_macos
conda install -y python=3.8
I specified the version 3.8 so that it matches requirement of tensorflow-macos.
Download and install tensorflow-macos from Apple Github.
git clone https://github.com/apple/tensorflow_macos.git
cd tensorflow_macos/arm64
pip install --force pip==20.2.4 wheel setuptools cached-property six
pip install --upgrade --no-dependencies --force numpy-1.18.5-cp38-cp38-macosx_11_0_arm64.whl grpcio-1.33.2-cp38-cp38-macosx_11_0_arm64.whl h5py-2.10.0-cp38-cp38-macosx_11_0_arm64.whl tensorflow_addons-0.11.2+mlcompute-cp38-cp38-macosx_11_0_arm64.whl
pip install absl-py astunparse flatbuffers gast google_pasta keras_preprocessing opt_einsum protobuf tensorflow_estimator termcolor typing_extensions wrapt wheel tensorboard typeguard
pip install --upgrade --force --no-dependencies tensorflow_macos-0.1a1-cp38-cp38-macosx_11_0_arm64.whl
Add the environment as jupyter kernel.
pip install jupyter
python -m ipykernel install --name=tf_macos
Then merely switching the ipython kernel to tf_macos allows us to use mlcompute-backend TensorFlow without a hassle
Install additional packages (optional)
conda install scipy pandas matplotlib
For more information you can refer here

Cannot import tensorflow in vscode

I have installed tensorflow in a virtual environment and can import Tensorflow in jupyter notebook or terminal. I am now learning to use VScode, so I try to launch it in the jupyter notebook within the VScode, but Tensorflow cannot be imported.
It shows ModuleNotFoundError: No module named 'tensorflow', but I have installed it and it can be used in the terminal or jupyternotebook.
I also tried to define the path of the python interpreter, but it did not work. Could someone help me out? Tons of thanks.
Try:
This works for me.
pip --version
python -m pip install --upgrade pip
pip3 install tensorflow==2.0.0
pip --version
python -m pip install --upgrade pip
pip3 install tensorflow==2.8.0
Worked for me.

How do I install the shap module on Mac?

I am trying to install the shap package and keep getting the following error
ModuleNotFoundError: No module named 'shap'
I have typed the following into my notebook as well as terminal
!conda install -c conda-forge shap
!conda install shap --yes
Here are the lines of the that are erroring out
!pip install shap
import shap
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X)
I am currently on a Mac
I got same issue using pip install shap command. However, conda install worked. I use Mac.
conda install -c conda-forge shap
I am on Mac OS Big Sur with pip 21.2.4 and Python 3.8.9. I am able to install shap with the following command.
ARCHFLAGS="-arch x86_64" pip3 install shap
This about a similar issue.It may help you.
Modules are installed using pip on OSX but not found when importing
If the above doesn't work, Try this:
uninstall and reinstall SHAP
Update Numpy to the latest version, then:
pip install git+https://github.com/slundberg/shap.git
if the above doesn't work, then maybe this:
To install shapely (https://pypi.org/project/Shapely/)? In python, you can install shapely by doing
pip install shapely
For windows shapley can be installed by downloading .whl from http://www.lfd.uci.edu/~gohlke/pythonlibs/#shapely and do
pip install <name of whl file>
or if you are using anaconda you can use conda-forge to get shapely
conda config --add channels conda-forge
conda install shapely

Gluonnlp installation not found on Sagemaker jupyter notebook

I am attempting to install Gluonnlp to a sagemaker jupyter notebook. Im using the command !sudo pip3 install gluonnlp to install. Which is successful. However on import I get ModuleNotFoundError: No module named 'gluonnlp'
I got the same issue when attempting to install mxnet with pip in the same notebook. It was resolved when I conda installed mxnet instead. However conda install has not been working for gluonnlp as it cannot find the package. I can't seem to find a way to conda install gluonnlp. Any suggestions would be highly appreciated.
Here are some of the commands I have tried
!sudo pip3 install gluonnlp
!conda install gluonnlp --> Anaconda cant find the package in any channels
!conda install pip --y
!sudo pip3 install gluonnlp
!sudo pip3 install gluonnlp
!conda install -c conda-forge gluonnlp --y
All these commands on my import
import warnings
warnings.filterwarnings('ignore')
import io
import random
import numpy as np
import mxnet as mx
import gluonnlp as nlp
from bert import data, model
result in the error
ModuleNotFoundError: No module named 'gluonnlp'
this is as simple as creating a Jupyter notebook using the 'conda_mxnet_p36' kernel, and adding a cell containing:
!pip install gluonnlp

Install utils package in python facing with error Package not found

I tried to install utils Python package using Anaconda but I ended up with this error:
Package missing in current win-64 channels
I tried:
conda install -c bioconda utils, but still the same. Can anyone help me fix it?
to install run:
pip install python-utils
to import:
import python_utils
worked fine on jupyter notebook ubuntu 16.04
Install:
pip install utils
Import:
import utils
Try run:
conda install --channel bioconda/label/bioconda-utils-test package
or
conda install -c conda-forge python-utils
I succeed using following instructions:
The package can be installed through pip (this is the recommended method):
pip install python-utils
Or if pip is not available, easy_install should work as well:
easy_install python-utils
for more details please refer to https://pypi.org/project/python-utils/
however, I failed using Влад Давидченко's answers.
Once you have installed python-utils with:
conda install --name myenv python-utils
You can then reference the package by using:
from python_utils import *

Categories