What does the term: Python -m mean? - python

I'm trying to run jupyter notebook.
For some reason, I get the error that jupyter is not a command :(
I looked at Running jupyter via command line on Windows user6094431's answer.
What does Python -m mean?
As in python -m notebook.
I used pip to install Jupyter.
"jupyter notebook" doesn't work btw.

The -m you're asking about is one of the command line flags that the Python interpreter recognizes. It tells the interpreter to take the next thing on the command line and treat it as a module name, like in an import statement. The named module will be used as the main module in the interpreter, like a script.
If you want to follow the advice of that answer, you need to go to an command line, and type in, literally python -m notebook. The first word is the program to run, the -m and notebook are arguments.
On Windows, the Python interpreter might not be in your PATH by default. If that's the case, you can instead try running py, which is a helper program that can run any installed Python interpreter. If you have more than one interpreter installed, you might need to pass a flag telling py which version you want (e.g. py -3.7 -m notebook). Extra arguments to py will be passed on to the Python interpreter, so -m should work fine.

Related

I keep getting "ModuleNotFound" error when i try to run code on my terminal even though i installed it

i work with:
Atom as a text editor
GitBash as a terminal
i've been trying to work with the numpy module but, even though i installed it via conda command on my terminal, it still gives me the ModuleNotFoundError when i run my code on the terminal
For example, here's some code:
https://i.stack.imgur.com/GL4JP.png
and here is me trying to run it on my terminal:
https://i.stack.imgur.com/ChOWm.png
As you have mentioned that you have installed numpy using conda, run your script or .py file in anaconda prompt (python yourfile.py) instead of GitBash or windows command prompt.
Otherwise, add python to the path and make sure that you are adding the right variable to path. If you are using conda installation of python, then add that to your path or if you installed python standalone, you should add that to path. Use following commands to know where is python installed in your system and add to path accordingly. Then use GitBash or CMD to run your files.
In CMD:
where.exe python
In GitBash:
which python
or
where.exe python

Python Modules are not being recognized by Bash (Cygwin)

I'm trying to run a python script in bash (with Cygwin on Windows 10), however it does not recognize modules like Matplotlib. For example, if I want to compile and run a script, I type in python text_reader.py and the result is ModuleNotFoundError: No module named 'matplotlib'. The script works fine in the CMD terminal and in Pycharm, but not in BASH. I believe the issue is that the the path of the python interpreters are different. If so, then how could I make the paths of the interpreters the same?
you need to install it most likely. have you tried.
pip install matplotlib
OR
pip3 install matplotlib
I have found the solution. I had to change the PATH environment variable to my python interpreter with the following BASH command,
$ export PATH="PATH TO PYTHON INTERPRETER"
and then I can normally compile a python file using a numpy or matplotlib module with
$ python text_reader.py

Can't get jupyter notebook to run when I put in python shell

When I type python --version in python 3.9 shell, I get python not defined.
The same command does not work when run from saved file or from shell.
When I type pip install jupyter I get invalid syntax.
Am I doing something wrong?
I just installed python and am just beginning with it and not sure why I can't get these to work.
Ok, found issue. You cannot get "python --version" by typing it into the python shell, I typed it into the command prompt for windows and it worked fine. Maybe I will have the jupyter thing figured out by tommorow.

VS Code can't find python 3 interpreter

I am installing Python 3.7.2 for the first time, and I'm using the VS Code python extension.
When I run python -V I get Python 2.7.10 which is not correct!
When I select the usr/local/bin/python3 interpreter in VS Code I get this error when running a script:
bash: /Users/erik/Work/Python/usr/local/bin/python3: No such file or directory
But when I look in usr/local/bin I can see that Python3 is there. I'm not sure why VS Code pastes the work directory in front of usr/local/bin ?
My first thought was that Python3 should be in the PATH variable so I ran the included Update Shell Profile command, which gives this feedback:
This script will update your shell profile when
the 'bin' directory of python is not early enough
of the PATH of your shell.
All right, you're a python lover already
Now, after rebooting VS Code I get a new option for selecting an interpreter:
/Library/Frameworks/Python.framework/Versions/3.7/bin/python3
Is that different from the Python in usr/local/bin ? When I select it, I get this error:
The script isort is installed in '/Users/erik/Library/Python/3.7/bin' which is not on PATH.
I also get this sideways related error:
You are using pip version 18.1. You should consider upgrading via the 'pip install --upgrade pip' command.
But, when following these instructions I get yet another error:
bash: pip: command not found
All in all, this process and the official documentation seem less than user-friendly? Apparently I'm required to dig deep through my mac's system files in the terminal before even writing one line of code.
Am I missing an essential step here?
I suggest that you use virtual environment for your project
first
pip install virtualenv
open cmd in your project directory that you open in VS-Code (it's important that vs-code sees this virtualenv folder that we will create)
mkvirtualenv my_env
and it will activate it automatically. if not run
my_env/bin/Scripts/activate or my_env/Scripts/activate
Then go open vs-code then select my_env for python interpreter
Well, if you want to change your default Python version for the whole system, it might break some applications that are depending on Python 2.
You can alias the commands by adding this line to your ~/.bash_profile:
$ alias python='python3'
The python command will now refer to python3.
If you want to execute the original Python (which refers to python2), you can escape the alias (so \python will launch python2 without touching the alias).
Btw.
$ unlink /usr/local/bin/python
$ ln -s /usr/local/bin/python3.7 /usr/local/bin/python
could also be a workaround for you.

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.

Categories

Resources