Can't figure out "unable to import [module]" in VSCode - python

I've recently swapped drives on my notebook and decided on a completely fresh install. When setting up VSCode for a new python project, I installed the latest version from python's website, added the appropriate environment variable to the path, removed the length cap for the pathname too, and all that.
python environment variable added to Path
But still, every time I import NumPy or any other module for that matter, it throws an error saying "unable to import [module]".
vscode error
I don't know what else to do, VSCode even recognizes the interpreter (see image 3).
python interpreter selected in vscode
Tried setting up a venv aswell, end up getting the same error.

This is how I normally set up a project and it always works.
(open directory in VSCode)
Within the VSCode Terminal
python3 -m venv .venv
source .venv/bin/activate
At this time vscode should automatically detect that there is a new virtualenv and ask you to use it.
If it doesn't you can use the CMD+Shift+P (CTRL+Shift+P) search for "Select Python Interpreter" and specify the path.

Did you install Numpy in the virtual environment (if you are using one)?
Try to list your installed packages in vscode terminal
python -m pip list
If numpy does not appear, install numpy with:
python -m pip install numpy
Does it work when you run the code? Or is this just a pylint problem?

Related

After two VScode updates the function import (cv or numpy) does not work, even selecting others python interpreter path

I often use python in VScode. However, after two VScode updates, the import function (cv or numpy) is not working. I've tried using the shift+command+p and > python select interprete but none are working for these functions.
I tried use > pip install pylint but it didn't work.
How can I solve this problem? Has someone the same problem in VScode?
It is recommended to use a virtual environment to manage third-party packages and python versions more conveniently.
Create a virtual environment with the following command
python -m venv myenv
After the command is completed, select the virtual environment interpreter just created in the Select Interpreter panel
The new terminal can automatically activate the environment
Now there is no package installed in the new virtual environment, and an error occurs
Install the cv2 package in the new environment, and the error disappeared after installing the package
Steps I would suggest to follow:
install the package again (cv2), even if it is installed.
check vs code if it has any updates for the python extension.
update vs code application.
restart vs code.
hope this helps.

Why can't Python find the module I installed?

Presently I'm using Python on a Windows system. I installed Python 3.10 from Anaconda and also the Pycharm IDE. I have ensured that Python is in the correct path in the environment variable. I have also replicated this problem using two different versions of Python, 3.10 and 3.9.
Very simply, in PyCharm, I open a terminal and type
conda install -c numpy numpy.
Then, I write a new "main.py" script. I have one line: "import numpy". I receive the error:
Traceback (most recent call last):
File "C:\Users\---\PycharmProjects\pythonProject3\main.py", line 17, in <module>
import numpy
ModuleNotFoundError: No module named 'numpy'
What am I doing wrong?
Going on advice from a friend, I created a new PyCharm project sitting not in my user directory but on the C: drive, and got the same error. Finally, when trying to re-install the package using either using either pip or conda, I get this message:
# All requested packages already installed.
You have 2 versions of Python:
Default Python (used everytime you open your command prompt and type python or python3)
Anaconda is installing packages in a virtual environment, using it's own Python (it is located in a different path)
You can see the path of your installed python using python -c "import os, sys; print(os.path.dirname(sys.executable))"
You have 2 Options:
Configure the PyCharm in order to use the anaconda Python. https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html#view_list
Open a command prompt in the project's folder (you can do it easily using PyCharm). Type conda env list. This will show you all available anaconda virtual environments. Choose 1 of them and type conda activate <env_name>, where <env_name>=the name of the environment. Then, run your program using python <name_of_your_program>
You can see the paths where the anaconda environments and packages are installed using conda info
There main reason for this is
You are running your main.py in different environment rather than where you installed numpy.
If you trying to run it via cmd use this method
Check which environment you are in right now. refer this and
this. But the most easiest way to do this is use where command
in windows cmd. C:\> where python or C:\> where python3. You will
get the path of activated interpreter.
list conda envs - conda env list
activate conda env - conda activate <env name>
then run this command. pip freeze . and check is there numpy in
the list. If not you have to find and activate the environment where
you have installed numpy.
If you want to run it in pycharm
Refer this on how to change pycharm interpreter.
https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html
Many things can cause this, usually its one of these
You may have to restart your terminal, or IDE if running in there, after installing a package to "refresh" the environmental path
The package is not in the environmental path

Can't import numpy module into python file, but it works with terminal

On Ubuntu I have python 3.6.9
I created new project using PyCharm, into project folder there is venv folder.
When I open venv folder in terminal and do:
python3
import numpy
It works, no any error.
But when I create some file in that venv folder using PyCharm, and try in this file:
import numpy
then I get ModuleNotFoundError: No module named 'numpy' error.
Why can't import module into file? module obviously installed because I can import that using terminal (folder is same for both cases).
What is my mistake ?
Pycharm uses virtual environments for serving modules, so you will have to download the package from either the GUI or by activating the virtual environment in the terminal and then doing the pip install.
if you want to do it from the GUI you can check the official tutorial. (Working and tested)
To do it manually, first to activate the environment in linux go wherever the venv folder is and type source venv/scripts/activate (to activate) and then you'll be able to do pip install numpy. (Not tested but it should work)
Could you share your Pycharm configuration?
Some extra steps are needed in Pycharm to configure venv, otherwise it will just take your normal interpreter site-packages.
Take a look at the Pycharm help page: https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html#existing-environment
looks like you need to install numpy through pip3.
Also, check which version of python is running in pycharm.

Sometimes Unable to Import NumPy

When I work in Jupyter Notebooks everything works fine, and I can import numpy and pandas successfully. However, when I try to download the script and then run it in an editor such as PyCharm or Atom, I get an import error: no module named numpy, and the same for pandas. How do I fix this? Is this due to the packages being installed in a different location than where I am downloading the code? Everything is installed with Anaconda, and when I try to do ```conda install numpy`` it tells me that all packages have already been installed.
This may be because Pycharm and Atom are using your default python install rather than your anaconda python environment.
You can configure Pycharm to use your conda environment via (https://www.jetbrains.com/help/pycharm/conda-support-creating-conda-virtual-environment.html).
Anaconda uses virtual conda environments to store the Python interpreter and libraries. If this isn't set up in your IDE, it won't see the libraries. This is described in this post: Use Conda environment in pycharm
Check your PyCharm interpreter options: File > Settings > Project > Project Interpreter. Make sure your desired Anaconda interpreter/environment is selected.
If your Anaconda environment isn't selected, click the Project Interpreter drop-down. if you see it there, select it. If not, click Show All... then + (Add) and browse to the Anaconda folder.
This post describes how to set up conda in Atom:
Using anaconda environment in Atom

Installing NumPy via Anaconda in Windows

I am trying to use Numpy in a Python 2.7.8 script in Windows. So, I download and installed the latest version of Anaconda (I also want to use some of the other tools in Anaconda) to the directory "C:/Anaconda". As default, I checked the boxing asking me if I wanted to add the path to the binary files to the Windows PATH.
After checking PATH, it contains the following: "C:\Anaconda;C:\Anaconda\Scripts".
Then, after installation I open a new Command Prompt, and in the Python envirnoment, I enter "import numpy", but this gives the error: "ImportError: No module named numpy".
What stage have I missed out in the installation process?
***** EDIT *****
I have just noticed that there is no file called "numpy" in either "C:\Anaconda" or "C:\Anaconda\Scripts". But according to the Anaconda website (http://docs.continuum.io/anaconda/pkg-docs.html), the installation should come with Numpy...
***** EDIT *****
It seems that Windows was running Python from an existing version of Python I had installed, which was not associated with NumPy. After deleting this, and running Python from the Anaconda installation, NumPy is now being recognised.
I had the same problem, getting the message "ImportError: No module named numpy".
I'm also using anaconda and found out that I needed to add numpy to the ENV I was using. You can check the packages you have in your environment with the command:
conda list
So, when I used that command, numpy was not displayed. If that is your case, you just have to add it, with the command:
conda install numpy
After I did that, the error with the import numpy was gone
Yep you should start anaconda's python in order to use python libs which come with anaconda. Or otherwise you have to manually add anaconda\lib to pythonpath which is less trivial. You can start anaconda's python by a full path:
path\to\anaconda\python.exe
or you can run the following two commands as an admin in cmd to make windows pipe every .py file to anaconda's python:
assoc .py=Python.File
ftype Python.File=C:\path\to\Anaconda\python.exe "%1" %*
after this you'll be able just to call python scripts without specifying the python executable at all.
Anaconda folder basically resides in C:\Users\\Anaconda. Try setting the PATH to this folder.
Move path\to\anaconda in the PATH above path\to\python
The above answers seem to resolve the issue.
If it doesn't, then you may also try to update conda using the following command.
conda update conda
And then try to install numpy using
conda install numpy

Categories

Resources