I'm trying to install and use python for the first time, but I'm having problems using extern librairies.
/usr/bin/python /path/core/__init__.py
Traceback (most recent call last):
File "/Users/fabienlamarque/Downloads/RedditAPI/core/__init__.py", line 6, in <module>
import requests
ImportError: No module named requests
Source code :
import requests
r = requests.get(r'path/.json')
As I surfed around trying to find answers, I found some people telling me to use pip, I indeed used pip and pip3 (with python 2.7.5 and python 3.4.2), the answer is always:
$ pip install requests
Requirement already satisfied (use --upgrade to
upgrade): requests in /usr/local/lib/python2.7/site-packages
Cleaning up...
Which seems like a good thing, I forced the reinstallation of the module several times, but I always have the same problem. My IDE tells me "No module named 'requests'" though.
What did I miss?
Thanks
I see /usr/bin/python in there so it's a *nix variant.
you will probably find that which python and which pip point to different locations.
You can just download the source and use python setup.py install assuming that your user has write privilages to the puthon site-packages dir.
Virtualenv for virtualenvironments of even better, conda environments is soemthing I highly suggest learning early on to avoid these headaches on your leaning path.
Related
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
I have installed the requests module
C:\Python34\Scripts\easy_install.exe requests
i got to the folder location
C:\Python34\Lib\site-packages\requests-2.13.0-py3.4.egg\requests
I have a path variable in system
C:\Python34\Lib\site-packages
yet when i run my script
C:\Users\beast\Desktop>update.py
I get the error No module named 'requests'
Traceback (most recent call last):
File "C:\Users\beast\Desktop\plex_playlist_update.py", line 17, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
I tried installing using pip just in case
python -m pip install requests
Requirement already satisfied: requests in c:\python34\lib\site-packages\requests-2.13.0-py3.4.egg
I am new to python and I cant find an answer anywhere.
UPDATE:
I found a command to check my python search location.
C:\Users\beast\Desktop>python -c "import site; print(site.getsitepackages())"
['C:\\Python34', 'C:\\Python34\\lib\\site-packages']
I think it has to do with C:\Python34 vs C:\python34? How do i check or fix this?
The problem here is maybe because of the different versions of python installed.
If you are a Windows user, you can go to Path in Environment variables and remove the paths to unnecessary versions of python (if any). Modules installed for one version of python won't work in another version.
So I ended up going with python environment. Per python documentation it is the way to go anyway. The below command are run on the root folder of the python app.
py -m venv env
then
./env/Scripts/activate
I then ran my pip upgrade and everything is working. Obviously this did not fix it computer wide. just for my python app. But from documentation this is better because of version control of the whole enviroment.
This creates a "env" folder at the root which will have all the packages installed. Need to install all requirements again or use the requirements.txt file.
I've been trying for a couple of hours already. It seems IDLE can't find any third-party module. I am a Python beginner.
Here is some info about my system:
OSX version: 10.11.5
python version: Python 2.7, Python 3.4, Python 3.5
The initial installation using pip (among other methods) seems to work fine. When I repeat the installation, terminal responds with:
Requirement already satisfied (use --upgrade to upgrade): pyperclip in
./anaconda/lib/python3.4/site-packages
However, when I go to IDLE (Python 3.4) and try to import the module, IDLE responds with:
Traceback (most recent call last): File "", line 1, in
import pyperclip ImportError: No module named 'pyperclip'
I have read that it may have something to do with my PATH or some virtual environment. I’ll be frank, I’m not sure what to make of these as they seem beyond my current ability.
This inability to import modules is becoming an almost insurmountable roadblock to advancing with Python. If you can offer any ideas on what I can do or can ELI5 the solution, I am forever in your debt?
It seems you are using conda, but you are trying to install the pyperclip module with pip. Have you tried running conda install pyperclip?
As stated here:
Because Conda introduces a new packaging format, you cannot use pip and Conda interchangeably; pip cannot install the Conda package format. You can use the two tools side by side but they do not interoperate either.
I wrote myself a handy bash script, which solves the task of creating a virtualenv with its own compiled virtualenv and python. It aims at creating a mostly self contained virtualenv, with maybe only native libraries installed in system level if necessary, but installing all python packages and virtualenv and pip and such things inside the virtualenv.
The script can be found here.
I invoke the script as follows:
self_contained_venv.sh \
-n udacity_model_building_and_validation \
-p https://www.python.org/ftp/python/3.4.4/Python-3.4.4.tar.xz \
-v https://pypi.python.org/packages/c8/82/7c1eb879dea5725fae239070b48187de74a8eb06b63d9087cd0a60436353/virtualenv-15.0.1.tar.gz#md5=28d76a0d9cbd5dc42046dd14e76a6ecc \
-d pandas scikit-learn seaborn
Given the required packages for compiling python and virtualenv are installed on the system, the script creates a nice virtualenv. However, when I try to access any installed modules/packages from within the virtualenv, python is not able to find them. To demonstrate this, I'll put some output of commands and code here:
First of all of course I have to activate the virtualenv:
. bin/activate
output: None, works without problem.
Then I print the pythonpath python is aware of:
import sys
for i in sys.path:
print(i)
output:
/home/xiaolong/development/Python/udacity_model_building_and_validation/lib/python34.zip
/home/xiaolong/development/Python/udacity_model_building_and_validation/lib/python3.4
/home/xiaolong/development/Python/udacity_model_building_and_validation/lib/python3.4/plat-linux
/home/xiaolong/development/Python/udacity_model_building_and_validation/lib/python3.4/lib-dynload
/home/xiaolong/development/Python/udacity_model_building_and_validation/localpython/lib/python3.4
/home/xiaolong/development/Python/udacity_model_building_and_validation/localpython/lib/python3.4/plat-linux
/home/xiaolong/development/Python/udacity_model_building_and_validation/lib/python3.4/site-packages
So far so good. Then I try to import a module / package I installed during usage of my bash script: pandas:
python
(IDLE is running)
import pandas as pd
output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'pandas'
Another try:
import numpy as np
output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'numpy'
Huh? So none of the packages is available? Lets check pip again:
which pip
output:
alias pip='localpython/bin/pip3.4'
./localpython/bin/pip3.4
Ok, so it's using my local pip.
Check packages:
pip list
output:
numpy (1.11.0)
pandas (0.18.1)
pip (8.1.2)
psutil (4.1.0)
Python-contrib-nbextensions (alpha)
python-dateutil (2.5.3)
pytz (2016.4)
PyYAML (3.11)
setuptools (18.2)
six (1.10.0)
virtualenv (15.0.1)
Hm the packages are there, so why can't python find them? Let's see where those packages are located, simply by trying to remove one:
pip uninstall pandas
output (shortened, because it fills many pages):
Uninstalling pandas-0.18.1:
/home/xiaolong/development/Python/udacity_model_building_and_validation/localpython/lib/python3.4/site-packages/pandas-0.18.1-py3.4.egg-info
/home/xiaolong/development/Python/udacity_model_building_and_validation/localpython/lib/python3.4/site-packages/pandas/__init__.py
/home/xiaolong/development/Python/udacity_model_building_and_validation/localpython/lib/python3.4/site-packages/pandas/__pycache__/__init__.cpython-34.pyc
/home/xiaolong/development/Python/udacity_model_building_and_validation/localpython/lib/python3.4/site-packages/pandas/__pycache__/_version.cpython-34.pyc
/home/xiaolong/development/Python/udacity_model_building_and_validation/localpython/lib/python3.4/site-packages/pandas/__pycache__/info.cpython-34.pyc
Aha, so the packages are in the path.
Another attempt on finding out if python looks in the right places:
>>> from distutils.sysconfig import get_python_lib
>>> print(get_python_lib())
/home/xiaolong/development/Python/udacity_model_building_and_validation/lib/python3.4/site-packages
So this one indicates it does not look in the right place, if I understand correctly.
Note:
The script does the following things, which might not be obvious:
compile python with a prefix (local python)
compile virtualenv for the local python
it aliases pip to the local pip of the version of the installed python
it aliases virtualenv to the locally installed one
it installs packages from pypi if specified
it updates the local pip if there is a newer version available
I am a beginner still at writing bash scripts, so I think the structure and logic of the script is fairly easy to understand. It also prints information about success of its operations in the terminal.
Further Notes:
I did not use su or sudo to run the script.
My OS is a Fedora 22, although I think in this case any major distro would work the same way.
OS has been updated recently.
Question: So why can't Python find them / its own packages? (What do I need to change?)
can you try to compare path to pyhton and pip in your script and manually in your system:
which python and which pip?
After that check pip freeze | grep pandas it should return you package ...if no you need to add this package to site-packages folder of you python.
So I'm trying to import AppKit into my python project. I am using pyCharm, but every time I try to import, I get the following error message:
You are using pip version 6.0.8, however version 6.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting AppKit
Using cached AppKit-0.2.8.tar.gz
Collecting flask (from AppKit)
Using cached Flask-0.10.1.tar.gz
Collecting pygobject (from AppKit)
Using cached pygobject-2.28.3.tar.bz2
Traceback (most recent call last):
File "<string>", line 20, in <module
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 20, in <module
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /private/var/folders/3p/csss5m7x30ldjd4z0xt6sg380000gn/T/pycharm-packaging0.tmp/pygobject
I've tried running the command directly through terminal etc, and I have upgraded my version of pip to 6.1.1 as well.
Not sure if this is a really beginner's mistake that I am making, or if there is something wrong with my installs of pip or python.
Any advice would be great, thanks :D
First, "importing" and "installing" are not the same thing. Installing is how you get a PyPI package, and all of its dependencies, onto your computer, so you can use its modules. Importing is how you use a module that's already on your computer.
Second, there are two different things named AppKit, and I think you've gotten them confused.
If you're on a Mac, you're almost certainly trying to use import AppKit to get the PyObjC wrapper around the native Cocoa framework named AppKit. If you don't already have PyObjC installed (I believe it comes with Apple's built-in Python on OS X 10.8-10.10), you get it with pip install PyObjC, not pip install AppKit.
If you're on Linux or FreeBSD or similar, you may be trying to import AppKit to get the AppKit framework for building GNOME GUI apps out of HTML and JavaScript. (Although this is in early stages, so I don't think you actually do want it.) For that, pip install AppKit is what you want. But you may need to install some system packages for GNOME development first with apt-get or urpmi or whatever's appropriate for your system.
If you're on Windows or something else, neither one of these makes sense, so… hopefully you're not. :)