This question already has answers here:
Why does "pip install" inside Python raise a SyntaxError?
(7 answers)
Closed 4 years ago.
I am quite new to python, and I'm trying to install pygame for a personal project, and the site recommends I use pip to install it. That's where I run into the problem.
When I enter
python3 -m pip install -U pygame --user
into the python shell, I get
SyntaxError: invalid syntax
It also highlights the 'pip' part of that.
The only thing I can think of is I don't have pip installed. But I am using python 3.6.2 and doesn't it already come with pip?
Am I stupid? What am I doing wrong?
You shouldn't enter that in the python console.
If you're in Windows, make sure you enter that in the Command Line (cmd).
Also, usually pip install package_name is enough, in which "package-name" is your "pygame". As said before, -U is to update an older version, so given those conditions, you don't need to put it there.
Hope it helps.
Pedro
PS: Also, --user makes pip install packages in your home directory instead, which doesn't require any special privileges. If it's what you want, go for pip install package_name --user, in which "package-name" is your "pygame".
I've never run pip this way. pip is a standalone program and is normally run from the command line not using the python shell. So run
pip3 install pygame
the -U switch is only needed if you have an old copy of pygame installed already which you need to upgrade. I've never seen the --user switch and can't find it in the pip documentation but I presume it is an attempt to install a local copy of pygame. In which case, unless you run pip as root (via sudo -H) (administrator on windows) you should get a local copy anyway.
This is similar to the way used on Windows when both python2 and python3 installed such as py -3 -m pip install package_name.
But on linux you don't need to do that.
You can use pip3 install package_name directly. And -U is used to update an older version.
Related
This question already has answers here:
pip or pip3 to install packages for Python 3?
(10 answers)
Closed 2 years ago.
Eventually, every single time I install a new Linux distribution I do sudo apt-get install python3.
However, once installed I always get confused. python is Python 2.7 and python3 is Python 3.x. But also it appears that pip is for Python 2 and pip3 for Python 3. That said most tutorials I see on Internet always use the traditional pip install even though it is about Python 3.
How should I deal with this? Should I simply continue to put this annoying 3 every time I use Python (pip3, ipython3, python3...)? In most of my lectures I read that creating a symlink python->python3 is a bad practice. Is that correct?
Use python3 -m pip or python -m pip. That will use the correct pip for the python version you want. This method is mentioned in the pip documentation:
python -m pip executes pip using the Python interpreter you specified as python. So /usr/bin/python3.7 -m pip means you are executing pip for your interpreter located at /usr/bin/python3.7.
Symlinking python->python3 is a bad idea because some programs might rely on python being python 2. Though, I have seen some Dockerfiles symlink python->python3, like TensorFlow's CPU dockerfile (it's less of an issue in a Docker image). Coincidentally, that same Dockerfile uses the python3 -m pip install syntax that I recommend.
creating a symlink python->python3 is a bad practice. Is that correct?
Sometimes. Some OSs (looking at you, macOS) deeply rely on python pointing to a Python 2 interpreter for internal tools and tasks. Deleting the shipped Python 2 interpreter (or aliasing python to a Python 3 interpreter) will break stuff. How to uninstall Python 2.7 on a Mac OS X 10.6.4?
Whether the correct command for Python 3 is pip or pip3 or (say) gaschplutzga depends on a number of factors.
If you only have Python 3, and you have a command named pip, that's probably safe to use. Going forward, this will be the simple, obvious, safe answer in more and more places.
If you have both, and there is a command called pip3 installed on your system, probably that's the correct one to use.
More generally, you can go through your PATH and look for commands with suitable names. On Unix-like systems with a POSIX-compatible shell, try the commands command -V pip3 and command -V pip. (On Windows systems, maybe try where pip3 and where pip, or pray to whatever dark deity informed your choice of operating system.)
If you receive output like
/opt/random/nonstandard/whoa/pip
/usr/local/bin/pip
/usr/bin/pip
you can try each of these in turn with the full path and adding the --version option to have them identify themselves. When you specify the full path, you are bypassing the system's PATH mechanism entirely. For example,
/opt/random/nonstandard/whoa/pip --version
might identify itself as belonging to Python version 3.2.1. If that's the one you want, and it's at the top of your PATH, you can simply rely on the PATH to give you this version when you type just pip. If not, perhaps you can shuffle your PATH (but understand that this changes the resolution order for all commands in the directory whose position you change) or create a simple alias or wrapper which bypasses the PATH for this particular command in your personal account. On Unix-like systems with a POSIX-compatible shell, this might look like
alias pip=/opt/random/nonstandard/whoa/pip
(to persist this across sessions, you'd add this to your .profile or similar - for Bash, try .bash_profile if it exists; for Zsh, try .zshrc. The full scoop for each shell is more complicated than I can squeeze into these narrow parentheses); on Windows, you might be able to control this by setting the environment variable PY_PYTHON, but there's a huge can of worms behind that "might".
Some sites and OSes / distros have additional wrappers or conventions which introduce additional options; if you use a specific package manager, perhaps also study its documentation. (One common example is Anaconda, though I don't believe it affects the naming or location of pip specifically.)
Use virtual environments, then pip would be associated with the python used to create that virtual environment. Whether you use pip or pip3, it will be equivalent to python3 -m pip as mentioned in jakub's answer. Also, given that Python 2.7 is already EOL (which means you will most likely work with Python 3) and that pip install-ing things onto the system packages should be avoided, then a virtual environment would be helpful here.
For example, using pipenv:
$ pipenv --python=/usr/local/opt/python#3.8/bin/python3
$ pipenv shell
Launching subshell in virtual environment...
(TEMP) $ pip --version
pip 20.2.3 from /Users/me/.venvs/temp2-SbXvZiFd/lib/python3.8/site-packages/pip (python 3.8)
(TEMP) $ pip3 --version
pip 20.2.3 from /Users/me/.venvs/temp2-SbXvZiFd/lib/python3.8/site-packages/pip (python 3.8)
For example, using venv:
$ python3.8 -m venv .venv
$ source .venv/bin/activate
(.venv) $ pip --version
pip 20.2.3 from /Users/me/temp2/.venv/lib/python3.8/site-packages/pip (python 3.8)
(.venv) $ pip3 --version
pip 20.2.3 from /Users/me/temp2/.venv/lib/python3.8/site-packages/pip (python 3.8)
The virtual environment takes care of making sure pip or pip3 in this env refers to the pip from the correct Python version. You can then happily follow tutorials that still use pip install something (unless of course that tutorial refers to a Python 2.7 or a system-wide installation).
You can install pip through pip3 and this should resolve this issue.
$ pip --version
pip 19.0.3 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
Notice that pip here is of Python 2.7 (in this example).
You can then force pip3 of Python 3.X to install pip under itself.
$ sudo pip3 install pip --upgrade
Installing collected packages: pip
Found existing installation: pip 8.1.1
Not uninstalling pip at /usr/lib/python3/dist-packages, outside environment /usr
Successfully installed pip-19.0.3
Once you check this again, it should reference Python 3.X so you don't have to deal with
what is what.
$ pip --version
pip 19.0.3 from /usr/local/lib/python3.5/dist-packages/pip (python 3.5)
I doubt you'll want to use Python 2 after this, but if you do happen to work with Python 2 code, you can create a virtual environment to access those commands again. Otherwise, you won't have to worry about the pip or pip3 distinction after this.
Not really a duplicate of this question, but this helped me suggest this answer: Can pip (python2) and pip3 (python3) coexist?
Pip is for python version less than 3. and pip3 is used when you want to install packages for python version 3 or higher.
I have installed python 3.7 but still using and showing version of python 2.7 . I want to change it to 3.. I searched it but ı can't did it.
You should not change your syslink python to use python 3 because it is most likely that your system is using that syslink to python 2 for its own tasks and processes, if you change that, you may broke your system.
As Sammy said in a comment, you should use python3 to use that version.
On the pip side, it is probably that your python 3 does not have pip include (it should have it, but I have seen a lot of Python 3 without it). You can check if you have pip doing: python3 -m pip. The -m param is used to execute modules of python installed.
If you do not have pip installed, you can install it following this (which I recommend because always work): https://pip.pypa.io/en/stable/installing/
That is:
Download a script to install pip: curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
Execute the script in order to install pip (with sudo because you're going to modify your system): sudo python3 get-pip.py
Now you should have pip installed and doing python3 -m pip again should show you the help of pip.
If you have pip already installed and no pip3 command in your system, you can always create an alias to python3 -m pip with the name pip3 and problem solved. Also if you don't know or do not want to create alias or executables in the /usr/bin folder, you can always keep using python3 -m pip.
PS: It is highly recommended to use virtualenvs when developing with python. If you do not know what it is, here it is a link to the docs: https://virtualenv.pypa.io/en/latest/
This question already has answers here:
pip not working on windows python 2.7.9
(8 answers)
Closed 4 years ago.
I know there's a post like this, but I can't find the answer yet :S
I installed the last version of Python (3.7) and it's already come with pip right? I tried everything on CMD in intention to install libraries but everytime i got "Invalid Syntax".
py -m pip install ndjson;
python -m pip install ndjson;
pip install ndjson;
python.exe -m pip install ndjson;
even python --version got invalid syntax.
someone help me?
my windows is 64 bits..
EDIT:
enter image description here
You are in your Python interpreter rather than the terminal.
Open a new terminal and try pip install ndjson again.
Just so you know, if you see >>> at the start of your terminal, you're in the Python interpreter. You can't run pip commands from the Python interpreter; you can only run them from the terminal.
To exit the interpreter you can type quit() (or hit control D in some systems), which will bring you back to your terminal.
I'm trying to use neovim with deoplete and UtilSnips. Both requires Python support from nvim.
I followed the instructions in :help nvim_python to set the support but the output of :echo has('python') or :echo has('python3') are both 0.
On nvim-startup I get the message UltiSnips requires py >= 2.7 or py3 and for deoplete It requires Neovim with Python 3 support ("+python3").
My python (2.7.10) and python3 (3.4.3) are both installed with homebrew. The neovim module is installed over pip and pip3 with install neovim but nvim can't find it, even when I set the let g:python_host_prog path in nvimrc.
I don't know what I am able to do anymore, has anyone an idea whats wrong with it?
Please follow the instruction on https://neovim.io/doc/user/provider.html#provider-python to setup the python interpreter for neovim.
First, install pynvim (previously, it was named neovim, but that has been changed) plugin
pip3 install pynvim
Print g:loaded_python3_provider
echo g:loaded_python3_provider
" for python 2.x use the following
" echo g:loaded_python_provider
If it returns 1, the python is not setup for neovim. In your ~/.config/nvim/init.vim file, set the python interpreter
let g:python3_host_prog='/path/to/python3'
" for python2, use the following instead
"let g:python_host_prog = '/path/to/python2.7'
I encountered the same problem lately. Here are the steps adapted from answer of #VforVitamin where I made it working.
Assuming python2 and python3 are installed.
Install neovim plugin pip3 install neovim.
Open neovim.
Execute command :UpdateRemotePlugins.
Restart neovim.
I had the issue myself because I used neovim inside virtualenv. If so, make sure to pip install neovim inside your virtualenv, and make sure that import neovim works in the python interpreter.
If that doesn't help, you can try and run neovim with debug messages (neovim -V3, or any other logging level) and see what you can pick from there.
I bet you have a line in your init file that starts with "set runtimepath=".
Change it to "set runtimepath+="
If when you try
let [interp, errors] = provider#pythonx#Detect(2)
From the docs at
https://github.com/neovim/neovim/wiki/Troubleshooting#python-support-isnt-working
You get errors, could be that you have your VIM environment variable pointed to the wrong (probably vim74) runtime directory.
Neovim needs pythonx.vim from the runtime/autoload/providers/ folder to load a python interp. Vim74 doesn't provide this file.
If you have an env. variable of VIM (with a path), it will use that as your location of your runtime files - I had my set to /usr/share/vim/vim74, changing it to the location of neovim worked. I guess there is a compile time option to point to the correct location too.
I was with the same problem and the solution actually cames from the question.
What I did was:
pip install --upgrade pip
pip3 install --upgrade pip
sudo pip install setuptools
sudo pip3 install setuptools
sudo pip install neovim
sudo pip3 install neovim
After it, enter in neovim and :UpdateRemotePlugins. Close it and open again.
After these steps I had neovim working properly.
Edit:
For those using Arch Linux, we have a peculiarity about Python. The steps are:
Install pip (python3) and pip2 (python2): pacman -S python-pip python2-pip
Instead of pip3, you should use pip2
Beyond this minor difference, the rest of commands work pretty much the same way.
As pointed out by #fwalch, the documentation has changed to https://neovim.io/doc/user/provider.html#provider-python.
Neovim comes shipped with Python3 enabled, but you need to install the pynvim module in order to use Neovim Python3 plugins:
python3 -m pip install --user --upgrade pynvim
I thought that virtualenv was supposed to encapsulate and hide all of your packages that were already installed. But when I type
$sudo virtualenv -p /usr/bin/python3 testenv
$source ~/testenv/bin/activate
$sudo pip list
I get:
apt-xapian-index (0.45)
argparse (1.2.1)
chardet (2.0.1)
cmsplugin-filer (0.10)
colorama (0.2.5)
command-not-found (0.3)
debtagshw (0.1)
defer (1.0.6)
dirspec (13.10)
...and many more
Even with the --local parameter. Is virtualenv broken?
Also when I type: $ which pip I still get: /home/jelikraftuser/testenv/bin/pip Which seems correct.
Reading the answers in this post: pip installing in global site-packages instead of virtualenv
I found the suggestion to run pip directly with $sudo ~/testenv/bin/pip list and it actually worked, it only listed 2 packages. However when I run which pip it lists the pip in the virtualenv as being the one which would be run.
So I'm sort of lost at this point. Calling the pip list with the full path gives me the correct (small) list of two packages, and calling pip list without the full path gives me a giant list of packages, which is incorrect. So, where do I go from here?
How can I make it not recognise globally installed packages as being installed in the virtualenv when I run pip without the full path?
OKAY UPDATE! This is kinda interesting:
(testenv)$ pip --version
pip 1.5.6 from /home/jelikraftuser/testenv/lib/python3.4/site-packages (python 3.4)
(testenv)$ sudo pip --version
pip 1.5.4 from /usr/lib/python2.7/dist-packages (python 2.7)
When I run pip as sudo it runs one, and when i run it as non-sudo it runs a different pip. Why would it do that? And if I'm going to be installing a package, i'll be running it as sudo, so I need sudo to use the correct pip.
Second update:
Reading this stackoverflow:sudo changes PATH - why?
I found that on ubuntu you cannot change the path variable for sudo, but this still confuses me since it was working before... So I'm still confused. Insight anyone? Previously I could type sudo pip list in a virtualenv and get a near-empty list. Does it do the same for you?
EDIT 3: What else it does:
When I run sudo pip install --download-cache=~/.pip-cache -r piprequirements.txt
it says that everything is already installed but when I enter python I cannot import them, but when I run python as sudo I can import them. So superuser can see packages that are globally installed > but I need to use sudo to install packages > so I can't install packages that are already globally installed. Also when I try to run pip3 as sudo, it says sudo: pip3: command not found. So this is definately an issue with ubuntu and how the environment path changes when you run sudo. Is not everyone else running into this issue then? I'm sure lots of people are using ubuntu, no?