Using module packages in Pycharm - python

I am super new to python and am trying to import a number of modules including numpy and matplotlib in Pycharm.
I have run
py -m pip install --user numpy scipy matplotlib ...
in the command prompt, which appears to have run successfully. However running
import numpy
in Pycharm gives
ModuleNotFoundError: No module named 'numpy'
When I installed these modules through the terminal there was a line which read
Stored in directory: C:\Users...
but using
from C:\Users\... import numpy
gives a syntax error.
If I try to go through the project interpreter in settings and install with pip that way I get
AttributeError: module 'pip' has no attribute 'main'
which apparently is how pip works now, and is why I went through the command prompt in the first place.
If I do
sys.path.append(C:\Users\...)
I get the same syntax error.
Could someone please explain getting these modules to work in the simplest way?
I find coding to come quite naturally, but just getting started can feel like climbing a mountain.

Please do it in a virtual environment
virtualenv --python=3.6 myvenv
source myvenv/bin/activate
pip install numpy
Then go to PyCharm's settings -> interpreter, and select the myvenv interpreter.

If you want to configure the default interpreter:
Run PyCharm, in the welcome window click Configure > Preferences:
In the Preferences window, under the Project Interpreter option select your python version, for example for python3.6:
Click Apply and OK
Open your project, PyCharm should find the installed libraries.

Related

No module named 'sportsreference'

I pip installed the sportsreference package in my command prompt and it shows successful. Yet when I try to call it in my Jupyter Notebook it says no module named sportsreference.
Any help on what I'm doing wrong?
The interpreter that your Jupyter uses to run the cells is not the one you installed that package into. Remember you can have not only different versions of Python interpreter, but also different virtual environments associated with each Python interpreter in your machine.
So either install the package in the interpreter that Jupyter uses, or run the Jupyter from the interpreter you've installed that package into.
The simplest solution is to install it in a Jupyter cell with:
!pip install <your_package_name>
the ! allows you to execute command.
To check which interpreter currently runs your code you can check the output of:
import sys
print(sys.executable)

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

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?

"ModuleNotFoundError: No module named 'kivymd'" in .spec file

I already did pip install kivymd in my Python project. I also had the kivymd directory in my project.
I'm working with a Mac.
I created a spec file called "coinsnack4.spec" including the code below:
from kivymd import hooks_path as kivymd_hooks_path
However, when I try to package my python project with the spec file with the command:
pyinstaller -y --clean --windowed coinsnack4.spec
I got the error below:
File "coinsnack4.spec", line 3, in <module>
from kivymd import hooks_path as kivymd_hooks_path
ModuleNotFoundError: No module named 'kivymd'
I really don't know why this happens because I already pip install kivymd. I don't know what to do next and I would appreciate if anyone could help me with this error.
Thank you very much!
Why are you facing this issue?
The reason behind this is the concept of virtual environments in python. Each virtual environment is independent of the other. You can use different virtual environments, activate and deactivate them as per your project's requirements.
I would suggest you go through this doc once Python venv
As when you do a `pip install <SOME_PACKAGE> from your local terminal, it installs the package from into the default python environment and from the terminal itself (not pycharm terminal) if you try to execute the python program it will work fine but as soon as you switch to pycharm or any other IDE, it has it's own python environment set and that environment is unaware of what happened in the other python environment. So you need to install the pip package here also, in order to execute the same python program.
Solution:-
The first thing I would suggest is to install the package in the virtual environment that the pycharm is using. For that click on the Terminal icon the below bar of your pycharm window. then do run the below command :-
python3 -m pip install kivymd
If this doesn't work, try configuring the python environment in pycharm.
Below is how you can change or update your python interpreter in pycharm: -
Setting an existing Python interpreter
At any time, you can switch your Python interpreter either using the Python Interpreter selector or in the Project Settings/Preferences.
Creating a new Python interpreter
To add a new interpreter to the current project:
If you have a conda environment, follow the below steps: -
Or if you want to setup a new virtual environment, do as below: -
I think you installed pyinstaller not in project's virtualenv, just:
pip install pyinstaller
then problem will be fixed.

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.

How to properly install a module in python?

I 'm new to python, and I need some help.
At first i install pyperclip using command window: "pip3 install pyperclip". The command window responsed that I have downloaded the module successfully.
However I can not use pyperclip in my code. When I coded "import pyperclip", there was an error: "No module name pyperclip..."
I then changed the directory of the command window direct to the address of Python, but nothing seem to work.
I use PyCharm for my coding.
Hope you can hep me solve this problem.
Python responsed "No module name pyperclip..."
It clearly that I have download the module.
Find path to python which you are using and then do something like:
path/to/python -m pip install pyperclip
Or better use conda or pipenv
The problem is that you have installed the package for another interpreter, one that is not used by your current project.
How to install modules for a particular interpreter in PyCharm: https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-upgrading-packages.html
You can also select the interpreter for the particular configuration (setup used for running a script):
https://www.jetbrains.com/help/pycharm/creating-and-editing-run-debug-configurations.html

Categories

Resources