Import NumPy on PyCharm - python

I'm trying to import NumPy on PyCharm.
Using the PyCharm terminal and Miniconda I've launched the command:
conda install numpy
And this was the output:
Fetching package metadata: ....
Solving package specifications: ....................
# All requested packages already installed.
# packages in environment at C:\Users\...\Miniconda3:
#
numpy 1.10.4 py35_0
So I run my project but the terminal said:
ImportError: No module named 'numpy'
On my project bar I can see two different folders, the one with my project and another one with the external libraries.
Under External libraries > Extendend definitions there is a NumPy folder so I guess that the installation goes well.

Go to
ctrl-alt-s
click "project:projet name"
click project interperter
double click pip
search numpy from the top bar
click on numpy
click install package button
if it doesnt work this can help you:
https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-upgrading-packages.html

In PyCharm go to
File → Settings, or use Ctrl + Alt + S
< project name > → Project Interpreter → gear symbol → Add Local
navigate to C:\Miniconda3\envs\my_env\python.exe, where my_env is the environment you want to use
Alternatively, in step 3 use C:\Miniconda3\python.exe if you did not create any further environments (if you never invoked conda create -n my_env python=3).
You can get a list of your current environments with conda info -e and switch to one of them using activate my_env.

It seems that each project may have a separate collection of python libraries in a project specific computing environment. To get this working with numpy I went to the terminal at the bottom of the pycharm window and ran pip install numpy and once the process finished running the install and indexing my python project was able to import numpy from the line of code import numpy as np. It seems you may need to do this for each project you setup in numpy.

I have encountered problem installing numpy package to pycharm and finally figured out. I hope it would be helpful for someone having the same problem in installing numpy and other packages on pycharm.
Pycharm Setting :
Go to File => Setting => Project => Project Interpreter. On this window select the appropriate project interpreter. After this, a list of packages under the selected project interpreter will be shown.
From the list select pip and check if the version column and the latest version column are the same. If different upgrade the version to the latest version by selecting the pip and using the upward triangle sign on the right side of the lists.
Once the upgrading completed successfully, you can now add new packages from the plus sign.
I hope this would be clear and useful for someone.

Another option is to open the terminal at the pycharm & install it with pip
sudo pip install numpy

I added Anaconda3/Library/Bin to the environment path and PyCharm no longer complained with the error.
Stated by https://intellij-support.jetbrains.com/hc/en-us/community/posts/360001194720/comments/360000341500

In general, the cause of the problem could be the following:
You started a new project with the new virtual environment. So probably you install numpy from the terminal, but it is not in your venv. So
either install it from PyCahrm Interface: Settings -> Project Interpreter -> Add the package
or activate your venv and -> pip install numPy

Related

Editable module ModuleNotFoundError even if installed and running with python in command window

I am going completely mental as I fail to understand and keep my python environment running.
So :
I have a python environment running under Miniconda 3 and that I manage through an environment.yaml file
Environment is used to develop within VSCode
It installs a bunch of external package through conda and then installs my local development package in editable mode (simplified version of the environment.yaml)
name: DLL_ETL
channels:
- conda-forge
- msys2
- defaults
dependencies:
- pandas
- numpy
- pip:
- -e .
- datatable
I update it regularly through "mamba env update"
However, since today this is what I see :
Pip states that the package install is sucessful
In the environment, when running Python from the command line, I can import my package
Package is installed pointing to the correct location (a one-drive)
When opening up an interactive window in VSCode with that environment as kernel : package cannot be found
What am I doing wrong?
What I tried :
Deleting the environment and reinstalling from scratch
Deleting the pycache and dll.egg-info
Manually removing and installing again through pip (and not through mamba, even if it runs pip underneath).
conda list shows the package as properly installed
As can be understood :
Yes I am running a virtual environment
Yes I am in the correct directory (especially when updating through environment.yaml)
Even when manually installing
Yes the directory with my local install is in my Python path and python is running
I managed to find the solution, but no idea why. I found a comment somewhere (I believe : pip: module installed as editable not found) that there is an issue in how some files are created when installing as editable.
More specifically, when installing as editable, two files are created :
___editable___dll_etl_1_0_0_finder.py
editable.dll_etl-1.0.0.pth
both in the environment
Miniconda3\envs\DLL_ETL\Lib\site-packages
When comparing two environments I have, I noticed that the .pth created in the one that broke was not formatted similarly, the .py didn't even exist. More specifically
The original file
What it should look like
When creating the files manually, everything seems to work fine. I haven't tried to update since. Likely it's an issue with setuptools if I understand correctly.
Restart the jupyter kernel.
Add the path to the package above the import statement.
import sys
// path is the directory where the package is located, not the full path of the package
sys.path.append("path/to/package")
import dll_etl

Python module not found even though its installed

Does anyone else have this issue? For reference check this screenshot:
PIP is also up to date and I already tried reinstalling it but it didn't work neither. I'm using VS Code. It also worked just fine yesterday but today it didn't anymore for some reason.
Your mss package is installed locally, but you are using a virtual environment to run code.
Two solutions:
1. Switch the interpreter to the local interpreter, which is the c:\Users\Anwender\AppData\Local\.... in your picture
Ctrl+Shift+P to open the command palette, search for and select Python:Select Interpreter
Select the interpreter with the mss package installed
2. Install the mss package for your virtual environment
Select the virtual environment interpreter in the selection panel
Create a new terminal activation environment
Install mss package using command pip install mss
you can try installing mss outside of the venv and see if that works. also check that the venv you're using is the right one according to vscode
If you are using multiple versions of python or use any python version in appdata, without venv, you can try running "pip install mss" from elevated (administrator) cmd instance within python install directory/scripts, shown on the console image above. If it is not the right python version used in the project, same applies to any installed python version. If IDE is used check interpreter setting to determine which one is in play.
If you are using a virtual environment, you can install you package directly there. For windows:
py -m pip install mss
More reading
The other solution can be to add the path of your package to sys.path:
import sys
my_pkgs_path = r'/home/.../my_pkgs'
if my_pkgs_path in sys.path:
print('already in sys.path')
else:
print('not found in sys.path')
sys.path.append(my_pkgs_path)
print('Added to sys.path')

Issues installing Cantera on Python

I am trying to use Cantera on python. I downloaded Anaconda and followed the instructions on Cantera's website to install. Their website's tutorial says to write:
import cantera as ct
import numpy as np
When I run it, I get the error:
ModuleNotFoundError: No module named 'cantera'
I tried reinstalling everything.
The error ModuleNotFoundError: No module named 'cantera' implies that you have not succesfully installed the 'cantera' module. You stated you downloaded anaconda then followed the install procedure here?
Can you please show me what the output was for this, if there was an error or admin block then cantera was never installed and that is why you can't import it.
You might not be activating your environment. Activate it first in the command prompt and then try to import the modules.
Without knowing the complete detail, my guess is that when you start a new project with Pycharm, it offers to create a new environment for you (See the image below).
An environment acts as an isolated container of packages and multiple environments can be setup (over simplification). By default, when creating a new environment it will only install the default packages with Anaconda and my guess is that Cantera is not a part of that list.
Now you've got two options:
Specifically install the package in the new environment you created:
Click on File -> Settings -> Project -> Project Interpreter
Click the + button on the right hand side of the package list and then look for the package you want to install. It should be installed to your specified environment.
Change the environment to the default anaconda env:
Click on File -> Settings -> Project -> Project Interpreter
In the Project Interpreter drop down, select the default anaconda interpreter. You can look at the packages installed in that env and find if your specific package is there or not. If the package is still not there, you could go ahead and install it using the previous method.
I have gone through a large amount of pain to eventually conclude that:
Cantera 2.4 will work only with Python 2.7, and a new Python 2.7 environment within Anaconda can be used to install it, though I didn't find this written clearly:Cantera Release Notes
To install and run Cantera 2.4:
Create and activate a Python 2.7 environment: Anaconda Documentation
Run the command: conda install -c cantera cantera (first iteration may fail to solve) - Anaconda Documentation
View your Python 2 environment packages in in Anaconda and you will see it listed.

How can I get packages installed in venv to show up in PyCharm project?

I am trying to use a virtual environment (venv) as the project interpreter for a PyCharm project. The virtual environment is called venvtest and is set up within the PyCharm project directory. That is, the project directory is /Users/gitanjali/Desktop/plot_bridges2 and the path to the virtual environment is /Users/gitanjali/Desktop/plot_bridges2/venvtest.
I've installed a number of packages in venvtest through Terminal using pip. I'd like to be able to import three in particular -- basemap, matplotlib, and numpy -- to some code that I'm writing.
When I set the PyCharm project interpreter to be venvtest, those packages don't show up.
I've tried the following things to fix this problem.
I've made sure that the path to the venv is set correctly (/Users/gitanjali/Desktop/plot_bridges2/venvtest/bin/python).
I've called pip freeze --local from within the PyCharm project, and I get a list of all the packages I expect, since I installed them.
(venvtest) (base) DN0a229530:plot_bridges2 gitanjali$ pip freeze -- local
asn1crypto==0.24.0
backports-abc==0.5
backports.functools-lru-cache==1.5
basemap==1.2.0
...
six==1.12.0
tornado==6.0.2
urllib3==1.24.3
However, when I look at the Project Interpreter window in PyCharm, none of these packages show up.
I've tried recreating the venv and invalidating caches and restarting PyCharm, but none of these have solved the problem.
What next steps should I try to get PyCharm to recognize the packages that exist in venvtest?
Try installing those by "file =>setting=> project interpreter=>addsign on upright => type in the package u want to install on top => click install package on bottom=>wait for the installation =>try it out!"
if still can't work, try pycharm help

Visual Studio Code windows , Python Pandas . No module named pandas

I set the variables for Env. All necessary modules for Pandas operation are installed by pip.
I wanted to run the code to display it in "Output". I run with: Alt + CTRL + N(or right click -> Run code).
In output returns the message:
But when I run in the terminal (right click -> Run Python File in Terminal), the code works correctly.
I would like the code to run correctly in output(right click -> Run code), not in the terminal.
Any suggestions?
I had a similar problem in VS Code. I was not able to find modules like Pandas, Selenium etc. Imports were underlined with a red colour.
Fixed the issue by changing the version of Python 3.7.3 64-bit to version 3.8.1 64-bit.
Procedure:
Hit Ctrl + Shift + P
Select Python: Select Interpreter
Choose the latest installed version of Python (no more red underlines)
#Davdei It is good practice to work with a virtual environment, so you can test against different versions of Python.
To get the list of available Python interpreter use (⇧⌘P) on Mac
or alternatively, you can click the status bar
It seems that the module pandas is installed in a virtual envorinment which you are not accessing via VS Code.
I'd suggest you to install pandas in default python as well via
pip install pandas
This way the VS Code will work it out just fine.
Else:
In VS Code console, activate the virtual enviornment in which you have installed the pandas module, and then run it. It should work
This can also happen when the specific module is not installed using pip3, so simply installing the module using pip3 would solve the problem.
pip3 install pandas
I had a same problem and none of the above seems to solve the issue. Later found that it has to do something with the python interpreter. I created virtual environment and my terminal can see it, but not the python script file where the code exists. Therefore, it must be an interpreter problem. Changed the interpreter to correct location:
cd .venv/Scripts/
./activate
(Here ".venv" is my virtual environment name. It could be different too (eg. "venv")
With an existing installation via pip:
pip3 install pandas
This solved the problem.

Categories