Sometimes Unable to Import NumPy - python

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

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 isn't autocompleteworking in vscode for python external packages?

I am new to python and I guess pyCharm is the most preferred IDE for python but I want to stick to vscode if possible. I have the python extension installed in vscode and autocomplete works fine for default python packages but for external packages like pillow or PyPDF2, its not working.
I created a virtual environment where I installed the above 2 packages using pip and even checked if the packages are installed using pip freeze.
Pip freeze is showing:
Pillow==9.0.1
PyPDF2==1.26.0
Below is my project structure. I need to import PyPDF2 module from basic.py. Also, if it helps, I am using python 3.9.6. Any help or suggestions are greatly appreciated.
You need to select the interpreter by following the docs.
Through the python extension, you can set the interpreter so that the packages will be taken from the environment. The interpreter is basically the python that your virtual env uses so it has access to the python packages that you installed in the virtual env.
Here are the steps in case the docs change:
CTRL+Shift+P or Command+Shift+P depending on if you're on Windows or Mac respectively
Type Python: Select Interpreter after the >
Then you can either select the interpreter from your virtual env that you created or enter the path to the interpreter.
Did you select the interpreter inside the vscode?
Enter vscode and type
ctrl+shift+p
and type in command prompt
>python:Select Interpreter
and then activate the Interpreter.
This will do the trick for you.

Installed Python 3, now my Anaconda environment is messed up

I apologize in advance for my poor vocabulary - I do not know much about environments, paths, and things of the sort. I am on macOS Catalina.
I created a program using Spyder from Anaconda. My program uses packages like pandas and numpy which are built into Anaconda. I used to be able to run the program in Terminal with the command: $ python3 app.py.
However, I recently downloaded Python 3.9.1 from https://www.python.org/downloads/mac-osx/. Ever since then, I have been unable to run the program in Terminal because of missing package errors like
import numpy as np
ModuleNotFoundError: No module named 'numpy'
I went to my applications folder, right clicked on Python 3.9.1, and clicked Move To Trash. This did not solve my problem. I reinstalled Anaconda (I did not uninstall it - just simply installed again), but that also did not solve my problem. I am able to run the program in Spyder (from Anaconda), but I wish to run it in Terminal again.
I believe the solution is simple, but I am not sure what to do. I have tried searching and reading but I am not familiar with the terminology. I think I just need to reset the environment, but I am not sure.
Anaconda is used for creating closed enviorments, so you don't need to thrash your computer with global packages.
Imagine you have 2 different projects. Project A works only on python 3.2 and Project B works on 3.8.
That's where anaconda comes in.Managing enviorments with conda
conda create -n PROJECTA python=3.2
conda create -n PROJECTB python=3.8
Now activate env you wish to work with. For macOS
source activate PROJECTA
Now you should see (PROJECTA) instead of (base)
Now inside this PROJECTA you can install modules you require like
pip install numpy
and when executing .py file
move to dir with your app.py file and
python3 app.py
this will be opened in conda enviorment you created and activated, using modules you installed in this env.
You can still edit py file and execute it through shell, but it will throw errors if you try to run it from IDE without linking project to respected conda enviorment.
If you are using PyCharm Configure a Conda vir env in PyCharm
You must create separate environments for every projects or it will get messed up.
conda create -n name_of_environment python=3.6
You must not delete the python folder into trash rather uninstall it Python 3.9.1 and remove its path from the directory.
In short if you run python from terminal and it is not supporting your Installed anaconda packages . You should use anaconda prompt instead of CMD.
Because your anaconda is not added to path rather then it picks up the python 3.9 you have installed from Python 3.9.1 from https://www.python.org/downloads/mac-osx/. ( This is the python with separate environment then anaconda so it wont detect anaconda packages ).
When you run the cmd and enter python it runs the python that you downloaded and installed rather then Anaconda
At the time of installing anaconda it gives option to add conda variables to path you can select those at installing stage / or add manually

Module not found in pycharm (Windows)

I wanted to install Pytorch via anaconda and it worked but PyCharm can't find the module (
ModuleNotFoundError: No module named 'torch'
I also have CUDA installed but when I looked up to add a the package with pycharm it also gives an error. When I added the anaconda interpreter I can't run the code. I use Python 3.6
Ok I solved this problem: First install anaconda and open the prompt then type conda install pytorch -c pytorch and pip3 install torchvision. Then go to PyCharm and create an Project and set the Project Interpreter to the Anaconda one (there is in the path: \Anaconda.x.x\python.exe ). Then you go to the Run settings and click Run... and then you go to Edit Configurations and then you select the Project Default interpreter and apply and you should be done! Thanks to the ppl who helped me =)
In you are on ubuntu and have installed pytorch using anaconda then in the interpreter of the pycharm which is under
File -> Settings -> Interpreter you have to do following:
search for the conda where it is installed by doing where conda then in that you will find a python.exe in your environment folder provide the path of that in the python interpreter.
Try with this it should work as I was facing the similar issue on windows solved it using this procedure.
And probably where conda won't work if you don't have support for ubuntu commands in windows than you can go to the folder where Anaconda is there which probably will be in your Users -> 'Your User' folder in that search for you environment and in that go to your environment and find python.exe give this path in pycharm.

How to Install a Package in PyCharm when project interpreter is set to conda, and the package is not provided/listed by conda?

I installed pycharm on my computer. I set the project interpreter to acaconda3/bin/python because that is the python3 interpreter I used on my computer before installing pycharm. I was able to install all packages I need using pycharm's package installer except for pydicom which is not provided by anaconda. However, pydicom, one of the packages I need for my project, is not provided by conda and hence does not show up in the list of available packages when I search.
How do I install this package that is not available with conda?
Open Anaconda navigator
Open environment from side tab
Open your environment which you created or choose the default( seems
in this case)
Choose Open in terminal
Run pip command here.
OR run pip by going to directory anaconda3/Scripts directory
Since pydicom is supported by conda-forge channel it wont show up on Pycharm unless you add that channel to conda environment channels manually.
Run below command for the environment
conda config --add channels conda-forge
Then it should show up in Pycharm.
Once channel added you can run below command within environment
conda install pydicom
Reference:
pydicom.github
conda-forge
so/using-anaconda-within-pycharm
Using PyCharm 2020.2 I can do this without going to the terminal or Anaconda.
Go to "Settings->Project->Python Interpreter" (same place as VictorLegros went, but the UI is different now: note the + button at the bottom of the list of packages)
Hit the + button, search for your package in the new dialog and then click "Install Package"
I double checked in the Anaconda UI afterward, and - after click on Update Index.. and waiting a bit (not 100% sure that was necessary, but I didn't see it at first) - I can now see the package "scikit-learn" that installed via PyCharm.
(Note: I'm not using Anaconda to do anything but verify: the search and installation was all in PyCharm)
I had this problem and I figured out that from the python interpreter dialogue I had to click the green circular Conda icon to disable "Use Conda Package Manager" (above the list of packages). Then when I clicked to add a package, I found all the packages I needed, which I presume were installed with pip.
I was also able to install packages that Conda needed to handle like psycopg2. Hope this helps.
I don't know if it's identical on the Mac, but for Win 10 Pycharm, you can access the Terminal from:
View > Tool Windows > Terminal (Alt+F12)
From there, make sure the correct conda environment is active through:
conda activate <your_env>
Then, you can install a package as one normally would typing in the command line, e.g.
conda install -c conda-forge <some_thing>
The exact command changes if you're using pip or some other manager or repository location, but it's helpful to do it this way if you want to stay in the IDE.
Also, you can verify the package in present in File > Settings > Project: ... > Python Interpreter
You should see your manually added package listed here, even though you didn't install it through the GUI.

Categories

Resources