Importing a module in a script - python

So, I have been working in the jupyter lab and I have created a code that uses numpy.
In the jupyter I had no problem using numpy. I installed the library, and I used the command import to use such module.
However, I had to save my code as a py file. When I try to run my py file using my cmd, I receive this message:
ModuleNotFoundError: No module named 'numpy'
If i type in my cmd "pip show numpy" it shows me that I have numpy installed.
I also have tried to add to my code a line with:
pip install numpy
pip3 install numpy
python -m pip install numpy
python3 -m pip install numpy
conda install -c anaconda numpy
conda install numpy
and for every option I received a syntax error:
SyntaxError: invalid syntax
Can someone help me?

As mentioned in the comments section, it seems like your Python script (.py) was not using the correct environment. Please ensure that correct virtual environment is selected by doing conda activate name_of_the_kernel, where name_of_the_kernel should match the kernel used in your Jupyter notebook.

Related

No module name 'tensorflow'

The requirements for the code to run is
Python 2.7
TensorFlow 1.n
SciPy & NumPy
I have python 2.7.8 by checking python --version. Installing them on python website also lead to a message saying there is no software to install so I assumed that I already have python 2.7x
For tensorflow I have install it using pip3 install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.0.0-py3-none-any.whl
and for Scipy and numpy i did brew install numpy scipy ipython jupyter
I have a python env setup call mypython.
Using pip list I have the following modules install as shown in the image.
But still VScode prompt an error and say that No module named 'tensorflow'when i run the code file.
How do I solve this?
enter image description here
Install Python extension instead of MagicPython;
You're using mypython in integrated Terminal but in left bottom corner, you select the global one,which is python3.10.1 as interpreter. Open Command Palette and choose Python: Select Interpreter, select mypython then reload window,the error should go away
Think you might be using the wrong pip. Could you run following in your terminal, with your venv activated.
$ pip --version
$ pip3 --version
Try installing with pip instead of pip3

Numpy Error: this is the wrong setup.py file to run while trying to install Numpy

I tried to install Numpy library with VisualStudio Code (VS Code) used the terminal and official website for instructions
Even though I followed each step I keep getting "This is the wrong setup.py file to run error"
I tried to update every element to not get an error, deleted and installed NumPy files in the directories which are in site-packages, and my anaconda files (i use jupyter as well but I need to implement this on my VSCode editor).
I also tried to get in the NumPy file and tried
pip install.
python setup.py build_ext --inplace
I used this site's instructions as well to install NumPy:
here I tried :
python -m pip install --user numpy
but keep getting the same error. What am I doing wrong?
In the screenshot you provided, I noticed that the installed module "numpy" exists in the "python3.7" folder, not in the "python3.8" you are currently using.
This is where my environment and numpy are located:
It is recommended that you use the shortcut key Ctrl+Shift+` to open a new terminal, VSCode will automatically enter the current environment, and then you can use "pip install numpy" to install numpy into "python3.8".
Or you can switch the environment directly to the python3.7 environment that includes numpy.
If it still doesn't work, you can uninstall numpy and reinstall it. ("pip uninstall numpy", "pip install numpy")
Since we are using pip to install the module numpy, we can use "pip --version" to check the currently used pip version, the module is installed in this environment:

python:installing Numpy

I tried the command 'python -m pip install numpy' several times to install numpy on the command prompt but nothing happens. Can someeone tell what the problem is?
Try pip install numpy or pip3 install numpy
Or
Try a fresh installation of Python from the scratch. Download the .exe installer from their official website, and do 'check mark' the 'add environment variable' at the beginning.
Link: https://www.python.org/ftp/python/3.8.3/python-3.8.3-amd64.exe
Try this and see if it works
pip3 install numpy
On your CMD type python.
It will enter python and you'll see this sign (>>>), type 'import numpy'.
If it didnt install it you'll get:
ModuleNotFoundError: No module named 'numpy'
Then you'll need to type 'pip install numpy'.
If it did install it you'll see on a new line (>>>).
Another way to check if you installed it is by typing 'pip list' and see if it is there.

Error: ModuleNotFoundError: No module named 'torch' in spyder, works fine in jupyter notebook

I'm trying to learn python for a class. we're using jupyter notebooks where the import torch command works fine but using spyder I keep getting this error and I have no idea how to fix it. I'm a complete newbie at this and I really don't know what to do.
Let's try this
conda install torch # Use anaconda prompt
or
pip install torch # Use the command prompt
Open Spyder terminal and try running conda install torch if using Anaconda, or pip install torch otherwise.

Jupyter python3 notebook cannot recognize pandas

I am using the Jupyter notebook with Python 3 selected. On the first line of a cell I am entering:
import pandas as pd
The error I get from the notebook is, ImportError: No module named 'pandas'. How can I install pandas to the jupyter notebook? The computer I launched the Jupyter notebook from definitely has pandas.
I tried doing:
!pip install pandas
And it says it is already installed but for Python 2.7 at the bottom. My script shows it is a Python 3 script at the top though.
When I do echo $PATH in Ubuntu is shows that '/home/user/anaconda2/bin' is on the first entry. I think I may need to change this to be anaconda3?
UPDATE: When I try and launch a Python3 script through jupyter the command line which launched Jupyter gives me the error "ImportError: No module named 'IPython.paths'. Then there is a timeout waiting for 'kernel_info' reply. Additionally, I tried removing anaconda but still experience the same error. I have tried to make so many quick fixes now, that I am not sure what the next step is to get this working.
As your default python version is 2.x , if you don't have any emphasis on the python 3.x you can try from the first by the below scripts.
pip install --upgrade pip
pip install jupyter
then in jupyter notebook:
!pip install pandas
The version of notebook will be 2.x.
Otherwise install pip3 by the below Linux commands.
sudo apt-get install python3-setuptools
sudo easy_install3 pip
now you can add pandas to the notebook by !pip3 install pandas.
This worked for me
in Jupiter notebook
import sys
print(sys.executable)
copy path eg:
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
install the module on the terminal like this.
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3 -m pip install pandas
Or it can be installed directly from Jupyter Cell as follows
! /Library/Frameworks/Python.framework/Versions/3.7/bin/python3 -m pip install pandas
If you use anaconda already as a distribution, stop using pip in that context. Use conda instead and you will stop having headaches. The command lines and procedures for setting up a new environment are pretty well documented here.
Basically upgrading python or having specific branches:
conda update python
conda install python=3.5
Or using specific environments:
conda create -n py35 python=3.5 anaconda
simple step to resolve the problem is
( NOTE: Remember what you have selected python 2 or python 3).
for python 2+
!pip install pandas
or if you have user permission error type
!pip install pandas --user
for python 3+
!pip3 install pandas
or if you have user permission error type
!pip3 install pandas --user
If pip3 is install you could run
!pip3 install pandas
This is what i have done in my system:
I have installed both anaconda for python 2.7 and anaconda for python 3.5. Anaconda helps keep both the environment separate.
In Ubuntu:
The directory structure is like this:
anaconda2/bin/
anaconda3/bin/
Whenever i want to use python 2.7 i go to anaconda2/bin/ and create an environment or activate already existing environment and install or import all the necessary packages and same goes for python3.5 (go to anconda3/bin/ create or activate the required environment). This helps me keep things separate.
Since you are using anaconda you should first use "conda install " if that package is not found, then you can use pip install .
In Windows:
If you install both anaconda2 and anaconda3, its quite easy..
the shortcuts for anaconda prompt are in C:\Users\your-username\
there will be two folders anconda2 and anaconda3, you can start conda prompt for python2.7 from anaconda2 and python3.5 from anconda3
So, once you start the anaconda prompt you can just type "jupyter notebook" to open jupyter notebook in browser and import pandas(or any package).
You can check this link:
http://conda.pydata.org/docs/test-drive.html#managing-conda
I meet the same problem in jupyter notebook, and I run the command below and solve my problem:
!pip install pandas
For Windows
The first step is to create a new conda environment. A conda environment is like a virtualenv that allows you to specify a specific version of Python and set of libraries. Run the following commands from a terminal window:
conda create -n name_of_my_env python
This will create a minimal environment with only Python installed in it. To put your self inside this environment run:
source activate name_of_my_env
On Windows the command is:
activate name_of_my_env
The final step required is to install pandas. This can be done with the following command:
conda install pandas
To install a specific pandas version:
conda install pandas=0.20.3
To install other packages, IPython for example:
conda install ipython
To install the full Anaconda distribution:
conda install anaconda
If you need packages that are available to pip but not conda, then install pip, and then use pip to install those packages:
conda install pip
pip install django
Simple solution : In a Notebook's cell type and execute the code:
import sys
!{sys.executable} -m pip install pandas
Iuse window 7 for work and I had the same problems when I tried to import Pandas. So I tried to install packages under each environment:
Run cmd and type the following code:
activate py27
conda install pandas
If the system asks you do you want to install the following new packages, choose Y for [yes]
And install pandas for each different environment if you installed Python27, Python 35 and Python 36, as I did.
Then problem solved if you run jupyter notebook again and you can import pandas successfully.
You can also solve the same problem for packages like numpy, scipy, etc..
Maybe its a broken (pip) installation. Following worked for me:
sudo apt --fix-broken install
Followed by:
sudo pip3 install pandas
Hope this helps.
I was getting the error
modulenotfounderror: no module named 'pandas'
in jupyter.
I tried the command:
!pip install pandas
and it worked like a charm.
My pandas version was 0.20.3 I have updated to 0.25 using conda update pandas. I have checked in Command Line Interface(CLI) its pd.__version__ '0.25.1'.
In Jupyter notebook its showing '0.20.3'.
Please restart jupyter notebook. note is cache your pandas. or you can create a new notebook
One silly mistake that you could make is to invoke Jupyter with your local machine
python installation and not the anaconda python. I had the same problem and just
setting the PATH did not work for me.
FIX: By default, anaconda binaries are in "<Path_to_anaconda>/bin". For example, in my case, they are in "/home/klakhotia/anaconda3/bin". This directory will also contain the binary to anaconda jupyter. Launch the jupyter from here or create an alias in your .bashrc that points to this file.
What worked for me was:
pip3 install pandas jupyter

Categories