I am a PHP developer, tasked to write Python scripts.
The script uses requests, which I found to be the easiest Python equivalent to PHP cURL.
If you use cURL in PHP, cURL must be enabled on the server, otherwise, the script won't work.
Is there likewise any activation or enabling necessary in Python to use requests other than import requests?
You must also have pip installed on your computer to install the requests module, pip comes installed with python 3.4 and above so you could do something like "pip install requests"
Install requests on machine you are going to use it: pip install requests.
Note if you are on linux you might have 2 versions of python installed: python 2.* and python 3.*
If this is the case using pip install <package_name> will install it for python 2.*, so you should use pip3 install <package_name> to install it for python 3.
(I would also suggest using virtualenv, but this is another topic).
If package is installed then you can use it in your scripts by simply importing it: import requests
Nothing more is needed to enable/activate libraries(packages). It is the case for all packages in python. (install -> import)
Try this steps in order to use requests
If in Linux, you can $ pip install request from the terminal or $ pip3 install requests if you are using python3
Then, inside python interpreter,
import requests
url = "https://google.com"
res = requests.get(url)
print(res.status_code)
This will give you a 200 code
Related
I'm developing plugin Nrpe in Python.
When i try to execute my code it works well on my machine. But when the NRPE execute it, an error occurs : 'No Module named pycurl'
I'm working on CentOs6.10 with Python 3.4 and i've installed pycurl with easy-install, the path to pycurl.py is /usr/lib/python3.4/site-packages//usr/lib/python3.4/site-packages/pycurl.py
And my PYTHONPATH = ['/usr/local/bin', '/usr/lib64/python34.zip', '/usr/lib64/python3.4', '/usr/lib64/python3.4/plat-linux', '/usr/lib64/python3.4/lib-dynload', '/usr/lib64/python3.4/site-packages', '/usr/lib/python3.4/site-packages']
I hope somebody could help me ?
Thanks !
You probably need something like:
python3 -m pip install pycurl
You may want to use: python3 -m pip install --user pycurl
or a virtual environment.
pycurl doesn't come with python. You have to install it with pip from pypi.
When I run python -V from terminal, I see that Python 2.7.10 is installed. I want to keep this as the "global" version as OSX utilizes it.
When I run Idle, I see that Python 3.6.0 is running. How do I install libraries to this version of Python?
For example, if I run pip install bs4, the library is installed here beautifulsoup4 in /Library/Python/2.7/site-packages/beautifulsoup4-4.5.3-py2.7.egg - which is obviously Python 2.7.
So when I run my script from Idle, I get the following error:
ModuleNotFoundError: No module named 'bs4'
You want to use virtualenv. It sounds like you have two versions of Python installed and you need to focus on one while being able to manage the packages in each. Virtualenv will do this for you.
First install virtualenv (https://virtualenv.pypa.io/en/stable/),
Second run it specifying the version of python you want as so: `virtualenv -p /usr/bin/python2.6
Third you can use pip to install packages directly into this environment. This increases the amount of disk space you need, but will allow you greater control over your code.
When you have two versions of python, you will need to specify which version of python you would like to run. You can do this with the activate command. For example:
activate python3
Once you have activated the python 3 environment, you can then run pip:
pip3 install bs4
which will install the beautiful soup library in your python 3 environment.
Another answer was found here: https://stackoverflow.com/a/4910393/1580659
pipVERSIONNUMBER install will install the library to the correct version of Python.
$ pip2.6 install otherpackage
$ pip2.7 install mybarpackage
I need to use urllib module in my code and I import it like this:
import urllib.request
import urllib.error
but PythonAnywhere returns the following error:
> No module named request
It looks like the urllib library is imported successfully when I try:
python3 myscript.py
instead of:
python myscript.py
But in this case I get another error:
> No module named 'pyvirtualdisplay'
Pyvirtualdisplay is also needed in my code, so I dont know what to do. Can someone help ?
The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error
~ urllib2 - python docs
When you run your script using
python myscript.py
Your system is using python2 which doesn't have the urllib.request and urllib.error modules. Use the urllib2 library.
You need to install Python extensions into each copy of Python that you use. For example, python and python3 use different set of extensions. You may have a script called pip3 that installs extensions into your copy of Python 3.
Installation instructions on the PyVirtualDisplay project page state that first you'll need to install pip and Pillow for Python 3. If you were using a Debian or Ubuntu VPS, these might work in a terminal:
sudo apt-get update
sudo apt-get install python3-pip python3-imaging
sudo pip3 install pyvirtualdisplay
But a Google search tells me PythonAnywhere is a web application hosting service. The list of supported extensions includes pyvirtualdisplay in Python 2 but not in Python 3. Just a guess, but the administrators may not be aware that pyvirtualdisplay has been ported. I'd recommend contacting PythonAnywhere support and requesting the extension's installation into Python 3.
You can install pyvirtualdisplay yourself for python 3. Either use a virtualenv (there are details on the help pages) or use the --user argument to pip and ensure you use the correct version of pip (pip3.3 or pip3.4 depending on the version you want to use)
I'm trying to interface with an API using a python program that only works in 2.7 and not in 3.4 which is also installed on my machine. However, one of my program's dependencies is the requests module. I have requests available in my 3.4 environment, but in 2.7 import requests results in:
import error: no such module named requests
Many Stack Overflow Q&A's have reccomended installing requests for 2.7 using: pip2.7 install requests, but apparently I don't have pip2.7 because that results in:
'pip2.7' is not recognized as an internal or external command
Question: How can I get pip2.7 instead of my default pip? Is it a seperate version of pip or do I just need to tell my current version I'm trying to install for version 2.7 not 2.4?
Note: The suggested solution to use easy_install-2.7 -U pip to install pip2.7 doesn't run on my Windows7 maching, the command propmt prints : easy_instal-2.7 is not recognized as an internal or external command
By default 3.4 won't create the plain pip command but it can be enabled with an option which is presumably what has been done in your case. Since there are other names for the Python 3 pip you can safely overwrite it by installing the 2.7 pip on top of it with easy_install.
Since this is Windows, your Python binaries are kept in isolated directories so you should just ensure that the 2.7 directory is first in the path variable. Look in the <python root 2.7>\Scripts directory to see if you have an easy_install already. If not you need to install setuptools.
All I want to do is run a Python script that requires Python 2.7 & Requests on my Ubuntu 10.04 EC2 box.
I installed Python 2.7, no problem. "python" by itself still points to python 2.6, which is very annoying, b/c I'm not sure how ubuntu will freak if I change the symlink /usr/bin/python to point to 2.7.
I followed the (carefully buried) install instructions for pip (at http://www.pip-installer.org/en/latest/index.html, and which are WAY too hard to find if they aren't the ABSOLUTE FIRST command on the "install pip" page)
So, the real problem here is that pip install requests completes successfully, but only installs for python 2.6, not 2.7. The pip usage instructions say nothing about how to install a package for a specific version of python.
How do I do this?
I just want to run my python script that requires 2.7 + requests.
First install pip for your 2.7 distribution using easy_install (easy_install should definitely be included with your 2.7 distribution):
easy_install-2.7 -U pip
Then install what you need:
pip-2.7 install requests
Then you can run code with python2.7 instead of python.
Yeah it would be a bad idea to change the link pointing to which python version. Instead, can you change the shebang to say #!/usr/bin/env python2.7 instead of #!/usr/bin/env python ?
Though python2.7 /path/to/pip install requests might work; you should install pip for python2.7 separately instead.
If you don't use virtualenv then invoke pip as pip-2.7 (the command is available if you install pip for python2.7).
Follow installation instructions which is the first item in the table of contents. Substitute python with python2.7 in the instructions.