Can't import torch that could not be resolved - python

I'm trying to run this code take from https://github.com/ultralytics/yolov5. I installed all the files in requirements.txt, and when I check the pip list, I see the version of torch is available. I tried to run pip3 install torch but it announced "Requirement already satisfied". How can I fix this?
import torch
# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # or yolov5n - yolov5x6, custom
# Images
img = 'https://ultralytics.com/images/zidane.jpg' # or file, Path, PIL, OpenCV, numpy, list
# Inference
results = model(img)
# Results
results.print() # or .show(), .save(), .crop(), .pandas(), etc.
Thanks

Problems like this with Python are often caused by packages getting installed to the wrong instance of Python.
First off, are you using a virtual environment to run your code? If you are, make sure you're enabling the virtual env when you're installing torch.
You can use the following two commands to check if the pip you're invoking is from the same instance as where you're running your code. If you're using a virtual environment, make sure you enable it before running the commands.
pip3 --version
and
python -m pip --version
(Depending on your system, you might need to replace python with python3)
If they don't match, that means you're installing torch into the site packages of a different python instance to the one you're running your code in. If you find yourself in this situation, I would carefully look at your PATH variable to ensure that your virtual environment is coming first.
Alternatively if you just want to keep moving and the second command reports the correct location/version of python, you can just use python -m pip install to install into that virtual env.

Related

Error: Import cv cannot be resolved: How to install OpenCV correctly?

To keep a long story short. I copied this code from https://www.geeksforgeeks.org/detect-an-object-with-opencv-python/ (not really import but I still mentioned it)
import cv2
from matplotlib import pyplot as plt
# Opening image
img = cv2.imread("image.jpg")
# OpenCV opens images as BRG
# but we want it as RGB and
# we also need a grayscale
# version
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# Creates the environment
# of the picture and shows it
plt.subplot(1, 1, 1)
plt.imshow(img_rgb)
plt.show()
Anyways this line below has this error that says "Import "cv2" could not be resolved"
import cv2
When I run this code below to download opencv. I get the message that the "Requirement already satisfied: "
pip install opencv-python
Despit me closing vscode the reopening I still get that message that "Import "cv2" could not be resolved" Ive updated pip, I've printed the version of cv2 in cmd which I get 4.5.5, I've deleted python and python intelligence. I just cant to figure out why opencv isn't working. Any suggestions? (I'm a newbie lol)
According to your description, I think there should be multiple versions of Python in your system.
You could use "ctrl+shift+P" to choose your python interpreter.
You can also use pip's command to install opencv into the current Python package floder.
pip install -t FloderPath opencv-python
Two reasons come to mind:
1.) Either the path to the opencv library is not set in the editor so it cannot find it. That happens sometimes if you changed settings in the runtime environment.
2.) Or you created a virtual environment prior to installing opencv. In that case, install opencv for your virtual environment as well.
It is difficult to tell what is going on with so little information. The above causes are just the most likely. Check your runtime environment and your library paths.
u need to do several things to clear it up, open cmd and follow this,
step 0: of course u must check python is added in ur system variable path,two path is essential, for me two dir is like this (add for ur installation, if have added this just skip this step)
C:\Python\Scripts\
C:\Python\
step 1: clear current package from python site package
pip uninstall opencv-python
step 2: clear cache from pip , for fresh install
pip cache dir
u will get a list of dir printed, now browse to that dir using explorer and delete everything in there.
step 3: check pip package install dir for ur python, it should be like "..\python\lib\site-packages" , to check this type into cmd
pip list -v
this will give u dir reference of all site packages, and u should check whether it is
"..\python\lib\site-packages" or not.
step 4: reinstall the opencv , u can install community contrib version of opencv, this is extended package of opencv-python with extra modules
pip install opencv-python
or
pip install opencv-contrib-python
step 5: type python in cmd, if python idle responded in cmd, then ur system found python, then type import cv2 and make sure it is imported. If it is imported successfully then u need to make sure ur vs code python plugin in up to date and configured well, for me i have added python path to system variable and didn't have to configure the plugin, it works well.
let me know if u have the problem unsolved.

Installed PyTorch but VS code wont import torch

I have installed PyTorch by just using pip install torch. I also have the correct version of python installed (I don't have two different versions).
When I ran the following in VS code it returned the correct version, and when I check if PyTorch is installed with pip it works.
import torch
print(torch.__version__)
But for some reason VS code doesn't recognise torch when I try and import it, or try to inheret from nn.Module in a class.
I just get the error "Import torch could not be resolved" and "nn is not defined"
I'm really confused as to what to do as I cant find any other people having this issue and all PyTorch VS code examples I look at just install the python extension and have no issue.
Check if vscode is using the same python interpreter and environment in which pytorch was installed.
Hit cmd + shift + P and search for Interpreter. Click on Python Interpreter and choose the correct one.
Check the image shown below
Just selecting the interpreter in vs code won't work, you have to follow those steps.
(if you install PyTorch in an anaconda environment)
1-Open Anaconda Promote
2-Activate your environment (Conda activate --)
3-type Code -- (code) will open vscode
4-select interpreter Ctrl +shift +P then type Python:Selectinterpreter
5-select your anaconda env

How can I make this script run

I found this script (tutorial) on GitHub (https://github.com/amyoshino/Dash_Tutorial_Series/blob/master/ex4.py) and I am trying to run in my local machine.
Unfortunately I am having and Error
I would really appreciate if anyone can help me to run this script.
Perhaps this is something easy but I am new in coding.
Thank you!
You probably just need to pip install the dash-core-components library!
Take a look at the Dash Installation documentation. It currently recommends running these commands:
pip install dash==0.38.0 # The core dash backend
pip install dash-html-components==0.13.5 # HTML components
pip install dash-core-components==0.43.1 # Supercharged components
pip install dash-table==3.5.0 # Interactive DataTable component (new!)
pip install dash-daq==0.1.0 # DAQ components (newly open-sourced!)
For more info on using pip to install Python packages, see: Installing Packages.
If you have run those commands, and Flask still throws that error, you may be having a path/environment issue, and should provide more info in your question about your Python setup.
Also, just to give you a sense of how to interpret this error message:
It's often easiest to start at the bottom and work your way up.
Here, the bottommost message is a FileNotFound error.
The program is looking for the file in your Python37/lib/site-packages folder. That tells you it's looking for a Python package. That is the directory to which Python packages get installed when you use a tool like pip.

Solve atom error message

I received an error on atom, it asked to install ipkernal using pip.
Not sure what to do. I have Anaconda on my system and not pip. Can someone explains whats the error about and how can I solve it in using anaconda.
I was running a python code and saved the file as .py.
import pandas as pd
wd = pd.read_csv("winequality-red", sep = ";")
five = wd.head()
print ("five")
Error message:
No kernel for grammar Python found <br>
Check that the language for this file is set in Atom and that you have a Jupyter kernel installed for it.<br>
To detect your current Python install you will need to run:<br>
python -m pip install ipykernel<br>
python -m ipykernel install --user
This isn't really an answer, but you might have better luck on the dedicated Atom forums.
In your case though, it looks like you haven't installed the proper kernels Hydrogen needs to run Python with. (Of course, I'm just assuming you're using Hydrogen. You haven't actually provided any details about how you are trying to run it).
From the Hydrogen documentation, it takes you to this page for Python kernels.
https://nteract.io/kernels/python
In particular, I think you want to run the command conda install ipykernel

Getting the correct version of Pmw to install

Problem:
I'd like to install Pmw 2.0.0 (project page here) so that I can use it with tkinter in python3. The setup script from the package detects which version of python you're using and installs the version that is appropriate for your system (Ubuntu 15 in my case). I can't find any references to switches to make it install the 2.0.0 instead of 1.3.3(the Python 2.7 version), nor have I been able to get the script to install to the python3 libraries.
What I've done so far:
I've changed the python version detector in the setup script from
if sys.version_info[0]<3:
version='2.0.0' # really '1.3.3'
packages=['Pmw', 'Pmw.Pmw_1_3_3', 'Pmw.Pmw_1_3_3.lib',]
to
if sys.version_info[0]<2:
version='2.0.0' # really '1.3.3'
packages=['Pmw', 'Pmw.Pmw_1_3_3', 'Pmw.Pmw_1_3_3.lib',]
to attempt to force the installer to default to the python3 version, which it does, but it installs them in the python2.7 libraries (/usr/local/lib/python2.7/distpackages).
What I want to do:
I'm looking for a way to force the installer to put the 3.4-compatible package into the python3 libraries. If that means getting it to install both packages in their respective correct directories, that's fine, too. I'm stumped about what to try next.
Answered by RazZiel on AskUbuntu:
Link here.
Instead of using the command sudo python setup.py build and then sudo python setup.py install, I should have been using python3 to execute the setup script. I've managed to outthink myself pretty badly on this one.

Categories