Python importing modules in CMD - python

Hello thanks for looking.
i am trying to import third party modules but having a hard time.
I have already done the edit to the environment variable and i get python to show up in cmd but I can not find the correct syntax to do the install.
based on the python org site i should use python -m pip install SomePackage
i have also read that i should use pip install SomePackage
I have tried both and have not had success. I am in windows 10 below is the command prompt of my attempts.
C:\Users\T****J>python
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> -m pip install send2trash
File "<stdin>", line 1
-m pip install send2trash
^
SyntaxError: invalid syntax
>>> pip install send2trash
File "<stdin>", line 1
pip install send2trash
^
SyntaxError: invalid syntax
>>> python pip install send2trash
File "<stdin>", line 1
python pip install send2trash
^
SyntaxError: invalid syntax
>>> python -m pip install requests
File "<stdin>", line 1
python -m pip install requests
^
SyntaxError: invalid syntax
>>>
thanks again. searched the best i could and maybe i am missing something easy. I am new at this .

you are trying to give command line arguments within the interpreter...
If PIP is installed, you don't need to invoke the interpreter at all. Simply call:
C:\Users\T****J>pip install send2trash
from the command prompt.
otherwise, if you are using the pip module, you need to pass the command line arguments to the command line not to the interpreter...
C:\Users\T****J>python -m pip install send2trash

pip is a program itself, similar to how npm, bundler or nuget works.
What you are currently doing is firing up the Python interpreter, and writing the call to pip from there, it doesn't work like that.
What you're looking to do is to install a package with pip, like this:
C:\Users\T****J>pip install send2trash
and after that's done you can open the interpreter again and import the module like this:
C:\Users\T****J>python
>>> import send2trash
I strongly suggest, however, that you do a bit of research into virtualenv and how to use it, it will make your life easier on the long run

Related

Modules installed using pip, recognised by PyCharm, but not by macOS terminal

I am trying to access a python file created on PyCharm with my terminal. When I import modules on PyCharm they are found and successfully imported (e.g tkinter, PIL etc.). When I try to run the file on my terminal I get the following message :
Traceback (most recent call last):
File "THE_project_GUI.py", line 3, in <module>
from PIL import Image
ImportError: No module named PIL
The which python command returns : /usr/bin/python
The which -a pip command returns : /usr/local/bin/pip
How do make sure I use the interpreter that pip is installing for?
Trial:
tromgy's solution:
me#mahmouds-mbp-2 ~ % ln -s -f /usr/local/bin/python3.8 /usr/local/bin/python
me#mahmouds-mbp-2 ~ % cd /Users/me/Documents/Programming/THE_project
me#mahmouds-mbp-2 THE_project % /usr/local/bin/python3.8 /usr/local/bin/python THE_project_GUI.py
returns:
File "/usr/local/bin/python", line 1
SyntaxError: Non-UTF-8 code starting with '\xcf' in file /usr/local/bin/python on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
v0idbar's solution:
I have installed python 3.7 using :
brew install python#3.7
returns :
Python has been installed as
/usr/local/opt/python#3.7/bin/python3
Unversioned symlinks python, python-config, pip etc. pointing to
python3, python3-config, pip3 etc., respectively, have been installed into
/usr/local/opt/python#3.7/libexec/bin
You can install Python packages with
/usr/local/opt/python#3.7/bin/pip3 install <package>
They will install into the site-package directory
/usr/local/lib/python3.7/site-packages
I have changed the Pycharm interpreter by going to File> New project settings> Preferences for new projects. And then added 3.7 after finding it using /usr/local/opt/python#3.7/bin/python3.
Then I ran the following :
me#mahmouds-mbp-2 THE_project % /usr/local/opt/python#3.7/bin/python3
/usr/local/opt/python#3.7/bin/python3
Python 3.7.11 (default, Jul 6 2021, 12:43:19)
[Clang 12.0.5 (clang-1205.0.22.9)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> /usr/local/opt/python#3.7/bin/python3 THE_project_GUI.py
File "<stdin>", line 1
/usr/local/opt/python#3.7/bin/python3 THE_project_GUI.py
^
SyntaxError: invalid syntax
Try run pip -V to figure out what python version it is using:
$ pip -V
pip 19.0.1 from /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pip (python 3.7)
You can check if it the same version as the python executable you are using: python -m pip -V
$ python -m pip -V
pip 9.0.1 from /Users/lev/anaconda2/lib/python2.7/site-packages (python 2.7)
You probably have a mismatch between those versions.

Tensorflow installation error messages, 'SyntaxError'?

I'd like to install tensorflow using pip in python 3.7 on windows 10 using the command-line as administrator.
First time installing tensorflow and I get error messages:
(SyntaxError: unexpected character after line continuation character) or (SyntaxError: invalid syntax).
I have tried the various ways shown below:
# OPENING PYTHON AS ADMINISTRATOR
C:\WINDOWS\system32>C:\Users\plain\AppData\Local\Programs\Python\Python37-32\python.exe
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
# ATTEMPT 1
C:\> pip3 install --upgrade tensorflow
File "<stdin>", line 1
C:\> pip3 install --upgrade tensorflow
^
SyntaxError: unexpected character after line continuation character
# ATTEMPT 2
C:\> pip install --upgrade tensorflow
File "<stdin>", line 1
C:\> pip install --upgrade tensorflow
^
SyntaxError: unexpected character after line continuation character
# ATTEMPT 3
python -m pip install [tensorflow]
SyntaxError: invalid syntax
# ATTEMPT 4
C:\WINDOWS\system32>C:\Users\plain\AppData\Local\Programs\Python\Python37-32\> pip3 install --upgrade tensorflow
SyntaxError: unexpected character after line continuation character
# ATTEMPT 5
C:\WINDOWS\system32>C:\Users\plain\AppData\Local\Programs\Python\Python37-32> pip install --upgrade tensorflow
SyntaxError: unexpected character after line continuation character
Thanks!
python -m pip install tensorflow
works well on my windows7 system. I am currently using python3.6 64 bit version.
Before you execute "python -m pip install tensorflow" from commandline, set the "PATH" in environment variable to 'python.exe'

matplotlib works with default python but not python3.5 on debian

I have this issue. I think it's best described when I show you my bash commands that I used to produce the problem. See here:
josch#oogway:~$ python
Python 2.7.9 (default, Jun 29 2016, 13:08:31)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
>>> #works
josch#oogway:~$ python3.5
Python 3.5.0 (default, Apr 26 2017, 21:03:53)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'matplotlib'
>>> #what??
Matplotlib was installed by:
sudo apt-get install python-matplotlib
The "default" python came with my OS installation, python3.5 was downloaded and then compiled/installed from source.
Can anyone help? Tried to solve it myselve for 2 hours now but google can't find answer
Edit: I'm trying to install a "second pip" that works with my second installation of Python, which is Python3.5. My default-Python that is managed by the OS is Python 2.7.9. Now, as suggested I did:
joschua#oogway:~/Downloads$ wget https://bootstrap.pypa.io/get-pip.py
and then:
python3.5 get-pip.py
which gave me:
joschua#oogway:~/Downloads$ python3.5 get-pip.py
Traceback (most recent call last):
File "get-pip.py", line 20061, in <module>
main()
File "get-pip.py", line 194, in main
bootstrap(tmpdir=tmpdir)
File "get-pip.py", line 82, in bootstrap
import pip
zipimport.ZipImportError: can't decompress data; zlib not available
Try installing pip3 with:
sudo apt-get install python3-pip
and then try installing matplotlib with:
sudo pip3 install matplotlib
I actually could solve the problem now.
See this link for a reasonable description on how to run two different versions of python on the same system (and as #Pierre de Buyl suggested) use two different pip versions on the same system
https://www.linkedin.com/pulse/20140815081557-89781742-how-to-install-and-use-python-with-different-versions-on-same-linux-machine
So, you installed python3.5 from source. What version of Debian are you using? (I suppose Jessie).
If you want to install packages for this Python interpreter, you must have a pip that actually uses it. Two solutions:
It is installed. If so,
python3.5 -m pip
will work and you can install packages with
python3.5 -m pip install matplotlib
or
python3.5 -m pip install --user matplotlib
It is not installed. You must install it following the instructions at https://packaging.python.org/installing/#requirements-for-installing-packages
apt-get will not install the appropriate pip for your needs. It will only install a pip that is related to the python3 (3.4 for jessie I believe) that is also in the apt-get system.
EDIT: in light of the update, you need also to re-compile python3.5 with zip enabled.

'pip3 --version' failing with a SyntaxError

This all began when I set out to install the Requests library for Python 3 (I'm running on OSX Mavericks with Python 2.7.5 (installed by brew install python) and 3.4.2 (installed by brew install python3). When I run pip3 --version (or anything related to the pip3 command) I see this:
$ pip3 --version
Traceback (most recent call last):
File "/usr/local/bin/pip3", line 7, in <module>
from pip import main
File "/usr/local/lib/python3.4/site-packages/pip/__init__.py", line 11, in <module>
from pip.vcs import git, mercurial, subversion, bazaar # noqa
File "/usr/local/lib/python3.4/site-packages/pip/vcs/mercurial.py", line 9, in <module>
from pip.download import path_to_url
File "/usr/local/lib/python3.4/site-packages/pip/download.py", line 22, in <module>
from pip._vendor import requests, six
File "/usr/local/lib/python3.4/site-packages/pip/_vendor/requests/__init__.py", line 53, in <module>
from .packages.urllib3.contrib import pyopenssl
File "/usr/local/lib/python3.4/site-packages/pip/_vendor/requests/packages/urllib3/contrib/pyopenssl.py", line 49, in <module>
from ndg.httpsclient.ssl_peer_verification import SUBJ_ALT_NAME_SUPPORT
File "/usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/ndg/httpsclient/ssl_peer_verification.py", line 17
except ImportError, e:
^
SyntaxError: invalid syntax
When I run the Python 2.7.5 version I see this:
$ pip --version
pip 1.5.6 from /Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg (python 2.7)
Just for sanity purposes Here is what when I see when I enter the interactive interpreters:
$ python3
Python 3.4.2 (default, Oct 19 2014, 17:52:17)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.51)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
$ python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
A lot of the other answers related to updating pip3 suggest that I update pip3 with this commend pip3 install --upgrade pip which gives the same error, or I use easy_install -U pip but because of how brew sets up the Pythons, it only updates the Python 2.7.5 version (there is no easy_install3). Any ideas?
The root problem is that you somehow got a Python 2.x-only package installed into your 3.x site-packages.
Underlying that, you've actually got two different Python 2.7 installations (Apple's and Homebrew's) crossed with each other, which may have something to do with how you got a 2.7 package into 3.x as well…
Anyway, the reason this is breaking pip is that pip has various optional dependencies that it tries to import if present, and some of them do the same, and so on, and ultimately, starting up pip is importing the ndg-httpsclient package.
I'm not sure how you got that package. A standard Homebrew 3.x looks in two extra site-packages directories (fire up python3 then import sys; print(sys.path) to see all of the places it looks, both stdlib and site) beyond the one that pip3 installs into.
In this case, you've somehow installed the 2.x version of ndg-httpsclient into /usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/‌​python3.4/site-packages.
Since you didn't install it with pip—and, more to the point, since you can't run pip in the first place—you can't just pip uninstall it. So:
rm -rf /usr/local/Cellar/python3/3.4.2_1/Frameworks/Python.framework/Versions/3.4/lib/‌​python3.4/site-packages/ndg*
This could break some other packages that depend on it. Once you get things working, you can use pip3 list to see all the site packages you've installed and test them out. If you want to be paranoid, do something like this:
$ pip3 list > mypackages
$ rm -rf <each site-package directory>
$ brew uninstall python3
$ brew install python3
$ pip3 install -r mypackages
You might want to similarly clean up your Homebrew 2.7 (or just scrap it and only use Apple's—especially since I'm pretty sure you're running Apple's anyway), and the site-packages for the Apple 2.7 (but not Apple's Python itself, of course, because you can't uninstall that).

Unable to import praw module after install

Edit: Before you start deleting/modifying installs, please glance over StvnW's answer/summary to make sure you are applying the solution that is appropriate for you.
I've installed python 2.7.5 and pip [edit: mac OSX Mountain Lion.] I've run "pip install praw" in terminal. All good. When I run python and run "import praw" I get:
...$ python
Python 2.7.5 (v2.7.5:ab05e7dd2788, May 13 2013, 13:18:45)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import praw
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named praw
...same ImportError from a script.
when I install praw I get this:
$ pip install praw
Downloading/unpacking praw
Downloading praw-2.1.10.tar.gz (83kB): 83kB downloaded
Running setup.py egg_info for package praw
Requirement already satisfied (use --upgrade to upgrade): requests>=1.2.0 in
/usr/local/lib/python2.7/site-packages (from praw)
Requirement already satisfied (use --upgrade to upgrade): six in
/usr/local/lib/python2.7/site-packages (from praw)
Requirement already satisfied (use --upgrade to upgrade): update-checker>=0.6 in
/usr/local/lib/python2.7/site-packages (from praw)
Requirement already satisfied (use --upgrade to upgrade): setuptools in
/usr/local/lib/python2.7/site-packages/setuptools-1.1.6-py2.7.egg (from update-
checker>=0.6->praw)
Installing collected packages: praw
Running setup.py install for praw
Installing praw-multiprocess script to /usr/local/bin
Successfully installed praw
Cleaning up...
In python if I run help('modules') it isn't there.
Relatively new to python and I haven't been able to sort this out with google search. Any hints would be much appreciated.
Edit:
SitRep:
I've uninstalled 2.7.2, uninstalled praw, and uninstalled (homebrew) pip. I ran python 2.7.5 and it couldn't find the module (as you would suspect.) I then reinstalled pip with easy_install and now 2.7.5 is finding praw but giving this error:
$ python
Python 2.7.5 (v2.7.5:ab05e7dd2788, May 13 2013, 13:18:45)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import praw
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/praw/__init__.py", line 43, in <module>
from update_checker import update_check
File "/Library/Python/2.7/site-packages/update_checker.py", line 11, in <module>
from pkg_resources import parse_version as V
ImportError: No module named pkg_resources
>>>
Thanks for the help so far, the spurious install was the root of problem 1.
The solution to the final problem can be found here:
No module named pkg_resources
I wish there were some way to summarize this for future readers, but I've done so many things that I no longer recall what addressed what. I basically uninstalled everything (python 2.7.2, 2.7.5, praw, pip,) reinstalled 2.7.5 from http://python.org/download/, reinstalled pip with easy_install (and sudo command) instead of homebrew, reinstalled praw with sudo command, and followed the directions for the subsequent module error in the link above. Hope that helps. :)
The OS X system versions of Python live in /System/Library/Frameworks/Python.Framework/<verson>, each of which links to /Library/Python/<version>/site-packages. Seems like you have installed another version of python and/or pip to /usr/local, but when you invoke python you are still getting the system version.
Try this:
$ /usr/local/bin/python
>>> import praw
I'd also recommend looking into tools like pyenv and virtualenv if you are going to do any amount of work with Python. Pyenv lets you easily manage and switch between multiple versions (including micro versions x.x.3, x.x.5, etc). Virtualenv lets you create isolated Python environments where you can pin down versions of site-packages specific to a particular project.
Edit (summarizing):
sudo easy_install pip will install pip under /System/Library/Python/<version>. Calling that pip will install packages to /Library/Python/<version>/site-packages
brew install python will install a second version of python — including pip — under /usr/local/. That pip will put packages in /usr/local/lib/python<version>/site-packages/`.
One version may not see packages installed to the other.
which python and which pip are helpful for troubleshooting.
Dean's final solution above results in user packages being installed to the system site-packages.
Despite Dean's choice to update the system Python, many would advocate instead using brew, pyenv, and virtualenv to isolate oneself from the system Python.
It's strange. I am suspecting you have another version of python2.7 on your mac os
If you are using a mac. Check the version of python on your shell's path using which python. Then make sure that the shebang line in the script you are trying to run uses the same version of python.
In my terminal I typed
which python and got the output /usr/local/bin/python
I went to the script I was trying to run and added the following to the first line
#!/usr/local/bin/python
Then I went back to my terminal. Checked that I was in the same directory as the script I wanted to run and typed
./my_script.py
If you don't see any output at this point make sure that you have execution permission enabled for your script by typing chmod +x my_script.py in ther terminal. Then try running the script again using ./my_script.py

Categories