Unable to import boto3 in python 2.7 - python

I've tried importing boto3 in python3 it's working, but i've tried boto3 in python2.7, it is throwing following error.
python3
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 26 2016, 10:47:25)
>>> import boto3
>>> exit()
$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
>>> import boto3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named boto3
How we can make boto3 to work with python2.7 ?

Notice that packages are not shared between python versions. If you install a package in your python3.x local version, it doesn't mean the package will be installed in your python2.x local version...
First of all do the following:
pip freeze
If boto3 package isn't there, great! install it:
pip install boto3
if it is there then verify what pip is being used and make sure to use the pip linking to your python2.x version:
which pip
you can create a symlink to use pip for python2.7... or even better, use pyenv to manage your python versions and virtualenv to isolate your workspace for a given python version.
https://github.com/yyuu/pyenv
https://virtualenv.pypa.io/en/stable/

As answered already pip install boto3 will resolve this error.
I would suggest using one version of Python (either 3 or 2) for your application.
Even if the OS relies on python2, your application can make use of python3.
Anyways, python2 EOL is 2020 - so python2 will slowly diminish.

I've created new virtual env and activated it. It is working good in New virtual env.
virtualenv path/to/my/virtual-env
source path/to/my/virtual-env/bin/activate

Related

How to tell which version of python a CLI uses?

I have quite a few versions of python installed (running macOS). I installed scrapy with pip install scrapy, and it succeeded. When I use it e.g.
scrapy startproject newProject
I see ModuleNotFoundError: No module named 'six', indicating that I need to install that module (six).
Note: I could easily fix the specific error by installing six for all versions of python installed, but solving that problem isn't what I'm trying to work out here.
Specifically what I'm after here is how to know what version of python a command line utility is using when it runs?
pip install will install package under pythonxxx/site-packages, the concrete location is up to which python version the pip used.
use pip -V to see the pip path and the related Python version. For you question, missing six module, pip install six should be enough, which will install six to the same Python version of scrapy.
After install scrapy, we could also enter scrapy shell, and use the below code to see where scrapy is
scrapy.__file__
To check the version of python from within a script or the REPL you can use the sys module.
For example:
>>> import sys
>>> sys.version
'3.8.5 (default, Jul 21 2020, 10:48:26) \n[Clang 11.0.3 (clang-1103.0.32.62)]'

I Get ImportError: No module named pathlib, even after installing pathlib with pip

This is my first time asking on this site, so sorry if my question is not layed out correctly
y#DESKTOP-MQJ3NCT:~/Real-Time-Voice-Cloning$ python demo_toolbox.py
Traceback (most recent call last):
File "demo_toolbox.py", line 1, in <module>
from pathlib import Path
ImportError: No module named pathlib
I have tried:
pip3 install pathlib
and:
sudo -H pip3 install pathlib
but continue to get the same error
I am using the windows store version of ubuntu 18 LTS and python 3.7
When it comes to python, it's quite easy to make the mistake of just running "python ...". When you install python on windows "python" defaults to the python 2.7 installation ( probably changed now that 2.7 is no longer supported) if it is installed.
Ubuntu has the links "python2" and "python3" which makes so much more sense but can still lead to confusion.
If you have a local python Virtual environment, the "python" command defaults to the global install on windows (to further confuse people).
I find it generally best to create my own links to the global python "python27" and "python36" to avoid these confusions.
Same goes for pip. It's best to call
python3 -m pip install ...
Also. PyCharm is the most amazing Python IDE in the world and it helps with so much.
Sorry, turns out it was a simple mistake!
Instead of:
python demo_toolbox.py
I should have used
python3 demo_toolbox.py
As Linux defaults to Python 2 or something incompatible with pathlib

ModuleNotFoundError: No module named 'ruamel'

I'm using a Kubernetes inventory builder script found here: https://github.com/kubernetes-sigs/kubespray/blob/master/contrib/inventory_builder/inventory.py
On line 36, the ruamel YML library is imported using the code from ruamel.yaml import YAML. This library can be found here: https://pypi.org/project/ruamel.yaml/
On my OSX device (Mojave 10.14.3), if I run pip list, I can clearly see the most up to date version of ruamel.yaml:
If I run pip show ruamel.yaml, I get the following output:
I'm running the script with this command: CONFIG_FILE=inventory/mycluster/hosts.ini python3 contrib/inventory_builder/inventory.py 10.0.0.1 10.0.0.2 10.0.0.4 10.0.0.5
Bizarrely, it returns the following error:
Traceback (most recent call last):
File "contrib/inventory_builder/inventory.py", line 36, in <module>
from ruamel.yaml import YAML
ModuleNotFoundError: No module named 'ruamel'
I have very little experience with Python, so don't understand how this could be failing. Have I installed the library incorrectly or something? From the documentation on the ruamel.yml project page, it looks like the script is calling the library as it should be.
Thanks in advance
In my case, I was installing this with pip3 install ruamel.yaml, and it was puting the package in /usr/local/lib/python3.9/site-packages/, but the python3 binary on the machine was pinned to Python 3.7, so trying to import that module was sending the ModuleNotFoundError message.
What helped to fix this, was to install the module with python3 -m pip install ruamel.yaml, running pip via the python3 binary makes sure it runs on the same version, in this case 3.7, and gets installed via the correct version number site-packages.
pip is set to point to the Python 2 installation. To install the library under Python 3, do pip3 install ruamel.yml.
you're using python 3 and want to use the package that is with python 2. Go to the directory where your python 3 is, navigate to Scripts and use the pip in there to install the needed library.
This helped me (adding version number to python):
CONFIG_FILE=inventory/mycluster/hosts.yaml python3.6 contrib/inventory_builder/inventory.py ${IPS[#]}
[python 3.10.x].
There is no package called ruamel.yaml
what worked is pip install ruamel-yaml

Import Request not found

I just started a new project and the client gave me a new Mac Book Pro along with some Python scripts that they developed. Unfortunately, I cant' seem to get the Python script to run correctly.
I am new to using a Mac.
When I try to run the Python program, I get an error at "import requests" line despite that I have installed the requests module via pip. It may be an issue with I am new to Python and Mac and downloaded the latest Python before I realized that 2.7 is pre-installed or it could be something completely different.
The first 2 lines in the file xxx.py seem to work
import csv
from bs4 import BeautifulSoup
import requests
Error:
File /Users/ad/Downloads/xxx.py, Line 5, in <module>
from import requests
ImportError: No module named requests
From Terminal:
ADs-MacBook-Pro:Downloads ad$ pip3 list
pip (1.5.6)
requests (2.4.1)
setuptools (2.1)
ADs-MacBook-Pro:Downloads ad$ echo "$PATH"
/Library/Frameworks/Python.framework/Versions/3.4/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
Any suggestion would be appreciated...
By the way, from the downloads folder that I am running the xxx.py file from I get:
$ python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
...
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named requests
But from the folder that has requests library
ad$ pwd
/Library/Python/2.7/site-packages/requests-2.4.1
I get it to work in TERMINAL
requests-2.4.1 ad$ python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
...
>>> import requests
So I suspect its a PATH or environment variable setup error.
When you use pip3, you're using the pip associated with python 3.x. You need to execute your script with the related python 3. Try this:
python3 yourscript.py
There is a few issues on this problem.
Are you using virtualenv? If yes, maybe you are forgettting to activate it.
You said that you installed packages using pip3, but trying to run script using Python2.7. Install packages using correct pip version or run script with Python 3. As #josh-smeaton already pointed.
You should add this line below in the first line of your script.
#!/usr/bin/env python
Or this for Python 3
#!/usr/bin/env python3

I can't call pymongo in the python shell

I'm trying to use pymongo in the IDLE shell on MAC OS X 10.9, but I can only do it in terminal.
when I call import pymongo in IDLE I get the following error:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
import pymongo
ImportError: No module named 'pymongo'
but I use python in terminal I get this:
$ python
Python 2.7.5 (default, Sep 12 2013, 21:33:34)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymongo
>>>
I can then use it with MongoDB.
Have I made some sort of install error? I'm doing this to learn mongoDB so I'm relatively new to this. Any help is much appreciated.
From the information you supplied in the comments, it appears you have installed a version of Python 3.3.4 on your system but you have installed the PyMongo distribution to an instance of Python 2.7, probably the Apple-supplied system Python 2.7 shipped with OS X 10.9. When you install a third-party package (or "distribution"), it is normally associated only with the Python instance that you used to install it. There are several common ways to install such packages. One way is to use the easy_install command, as is suggested on the PyMongo page. However, the easy_install command is also associated with a particular Python instance. On recent OS X releases, Apple supplies easy_install commands that are associated with and install into the system Pythons. So it's a common pitfall on OS X to install a newer version of Python alongside the system Python but then use the default easy_install command with the result that the package you want ends up installed in the wrong Python version. One solution is to install a separate version of easy_install for each Python version you install. The easy_install command is provided by the setuptools package. However, these days the recommended installer tool for Python is pip which provides more features than easy_install, including the ability to uninstall packages, and is actively supported in the community.
curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py
python3.3 get-pip.py
python3.3 -m pip install pymongo
python3.3 -c "import pymongo; print(pymongo.version)"
-> 2.6.3
You also need to install a version of pip for each Python instance that you use. There are other ways to invoke pip but, by using the way shown above, you know which version of Python you are using and you are less likely to end up with the situation you have now. There are other tools you can use in addition, like virtualenv, but, particularly on OS X, pip should be sufficient to handle most beginning use cases.

Categories