No module named 'sportsreference' - python

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)

Related

VS Code - No module named 'streamlit'

I am trying to use VS Code and I am importing some modules (for example : pandas and streamlit). The terminal of VS Code tells me "No module named pandas" or "No module named streamlit" whereas it is downloaded on my computer.
VS Code:
Terminal of my computer when trying "pip install streamlit":
Do you know how I could correct this mistake ?
You are in a different env, so before you can import, you will have to append the path of the installed packages to your working environment.
import sys
sys.path.append("/opt/anaconda3/lib/python3.9/site-packages")
import streamlit as st
Please ensure that the environment is consistent. pip installs the library into the real Python environment, and you use the virtual environment.
If you want to use it in the virtual environment, please use the
command conda install to install it.
If you want to use the real python environment, you can use shortcuts "Ctrl+shift+P" and type "Python: Select Interpreter" to change the python environment.

Using command line in Jupyter Notebook not executing commands in virtual environment

I need to use the command line in Jupyter Notebook in a virtual environment, but the commands don't execute in the environment itself. I'm using Anaconda Navigator to open Jupyter Notebook. For background, I use a Mac.
Whenever I do something like ! pip install xyz, it installs the package in the Anaconda directory for libraries (see the image). I made sure that the kernel is in the environment, so that's not the problem.
What's interesting is that if I try to import a package, I can only import packages from the environment itself, not the Anaconda libraries. In other words, installing packages doesn't occur in the environment, but importing packages does.
Image that shows the pip install
This wouldn't matter that much because I could download packages in the Terminal itself, but I also need to do things like in the image below in my virtual environment, which is not possible to do in the Terminal (or is it?).
Example of what I need to do
Any ideas on how to fix this?
EDIT: That error is fixed. However, I'm getting an error if I try to use a Python command: image of error. The first line just defines the file path for the script (the last item in the list is a .py file). I tried doing "%%python" followed by the command, but it doesn't work. Any ideas?

Can't run Jupyter notebook

I installed Jupyter Notebook using pip install notebook but I am not able to run it.
When I try runing using jupyter notebook, it gives me the CommandNotFoundException:
and
When I try runing using python -m notebook, it gives me the ImportError: cannot import name 'constants' from partially initialized module 'zmq.backend.cython':
I have added the following into PATH:
Path to python.exe present inside local folder.
Path to scripts folder present inside local folder.
Path to scripts folder present inside roaming folder.
All the jupyter dependencies(jupyter.exe, jupyter-core.exe, etc) are present in scripts folder inside 'Roaming' folder and not inside 'local' folder.
python version: 3.8.4
pip version: 20.1.1
Use: python3 -m notebook in the terminal.
I had worked with Jupyter Notebook and usually the cause for most of the cases is missing dependecy - Jupyter got a lot of them.
Please check if they are not missing by running:
python -m pip check
or
pip check
This will give you list of missed (sometimes there are versions problems) dependencies. Installing proper version with pip install <dep>==<version> should help.
Edited:
#Divyessh Maheshwari, if you have installed anaconda, then make sure you are running jupyter notebook in the anaconda prompt.
If you haven't and you don't want to install anaconda, you might need to downgrade Python according to this answer Is there a way to run Jupyter Notebooks Without Anaconda?

Jupyter notebook, how to execute system shell commands in the right conda environnment?

I'm currently experiencing some troubles with jupyter notebook and system shell commands. I use nb_conda_kernels to be able to access all of my conda environment from a jupyter notebook launched in base environment, and this works perfectly in most of my use cases. For simplicity sake, let's assume I have 2 environments, the base one, and one named work_env. I launch jupyter notebook in the base environment, and select the work_env kernel upon opening the notebook I'm working on.
Today I came across this line:
! pip install kaggle --upgrade
upon execution of the cell (with the work_env kernel correctly activated), pip installed the kaggle package in my base environment. The intended result was to install this package in my work_env. Any ideas on how to make shell commands execute in the "right" environment from jupyter notebook?
Try specifying the current python interpreter.
import sys
!$sys.executable -m pip install kaggle --upgrade
sys.executable returns the path to the python interpreter you are currently running. $ passes that variable to your terminal (! runs the command on the terminal).
Aliases expand Python variables just like system calls using ! or !! do: all expressions prefixed with ‘$’ get expanded. For details of the semantic rules, see PEP-215
from https://ipython.org/ipython-doc/3/interactive/magics.html
-m is used to run a library module (pip in this case) as a script (check python -h). Running pip as a script guarantees that you are using the pip linked to the current python interpreter rather than the one specified by your system variables.
So, in this way you are sure that pip is installing dependencies on the very same python interpreter you are working on (which is installed in your current environment), this does the trick.

When using Jupyter, I have to install python modules with Conda and not cmd prompt. Why?

I'm pretty new to programming so forgive my ignorance.
When installing certain python packages/modules in the cmd prompt I am able to import them fine when using Jupyter Notebook. But some modules Jupyter Notebook cannot import without installing via Conda first. Why is this?
The problem seems to be related to system and environment that you are using and not the programming :)
Since you are a beginner, let us understand the concepts first rather than solving the problem.
Python code is run on an interpreter that is installed on your machine.
Jupyter is a web application that takes code using a given language's interpreter. So Jupyter, on its own doesn't run your code. It uses the interpreter installed on a system(your local machine).
Conda is a package manager and also an environment manager. That means using conda, you can create a virtual environment on your machine and that virtual environment can have its own installation of an interpreter. This virtual environment can have its own copy of the packages/modules as well.
Now comes the best part: The jupyter notebook can be asked to use any of the interpreters including the ones installed on a virtual environment.
So most likely, you are running the jupyter notebook from an environment which doesn't have the required dependencies. So either run the jupyter notebook outside the environment or install the required packages in the environment where your jupyter notebook is running.
To know which environment is being used by your jupyter notebook, run below lines from the jupyter notebook cell:
import sys
sys.executable
If you don't get something like /usr/bin/python then the jupyter is running inside an environment. So you have to install all the packages/modules inside that environment only.

Categories

Resources