Installing and importing Python Modules onto OS X - python

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.

Related

how to automatically install dependent modules used in a python app

I just started learning Python and a bit confused about how packages are distributed and installed. I am aware of helper scripts easy_install and pip which can be used to install the dependent modules,howerver I am not clear how to do with programatically,can someone help me on this?
How to install dependent modules automatically when running python applications? I have a dependency on subprocess32 and other modules,I want to automatically install them if they are not present....
File "script.py", line 6, in <module>
import subprocess32 as subprocess
ImportError: No module named subprocess32
I have looked at some posts online below but not clear...really appreciate guidance here
locallyoptimal.com/blog/2014/03/14/executable-python-scripts-via-entry-points/
Python packages installation in Windows
Easiest way to handle module installs within a program is with pip:
import pip
pip.main(["install", "numpy"])
For example will install numpy and its dependencies. You can also specify versions using numpy=xxx, or upgrade by passing "-upgrade" between those two above.

Python not finding its Packages

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.

checking virtual environment modules

So I am following this tutorial and I am about to give up since i am working on a windows machine and I am pulling my hair out finding equivalencies.
I have a virtual environment, but when I try to run my code it dosent appear to be finding the local dependancies!!
(venv) PS C:\Users\Vince\skellys\Flask_skelly\venv> python Bartender_no_ui.py
Traceback (most recent call last): File "Bartender_no_ui.py", line 7, in <module>
import lxml
ImportError: No module named lxml
How can i check if the dependencies are there and how do i make sure my code is running from venv?? Am I just to much of a noob to be trying this tutorial anyway??
maybe relevant details- I had to use Conda rather than pip to install lxml, because pip couldnt get the dependencies of libxml2 and libxslt, but I get no module error for the other modules that I installed in the venv using pip as well
If you do
pip freeze
That should show you what modules you have installed in your virtualenv as long as you have it active.
Seeing as how you are working on windows I'd check out this site Its what I followed when I first got started.
Haven't used Conda much myself so not familiar with how it works but I'm guessing that won't install to your virtualenv. If you download lxml from here you should be able to install it into your virtualenv on windows.
its not the best solution but I figured out a hack around - I copied lxml from my c:\anaconda\pkgs folder. Code appears to be running fine. figures.

Using a module in python

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.

How do I install Python packages/ wxPython on Mac OSX?

I'm very new to python and any non-basic computer functions in general, but I'm having a very basic problem and I can't figure out how to fix it. Any time I download a module from the internet and try to import it in python, I get an error message. For example, I just downloaded wxPython after being instructed to do so on a tutorial program for Python I've been using, and after entering "import wx" I got:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
import wx
ImportError: No module named wx
How do I fix this so that python can find modules I download?
Thanks!!
Python version 2.7.3, and I downloaded wxPython from the download link on the website. Another thing I noticed: whenever I type in python setup.py install in the Terminal, I get:
/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
can't open file 'setup.py': [Errno 2] No such file or directory
Which seems to be another huge problem?
There are a few things you need to do to actually debug this:
Run python and check what version you are running.
type where python and figure out if you have multiple versions of python running at once.
With pip or easy_install, read the output to check where they are installing packages. It's somewhat likely that they are installing to the system-wide Python 2.6, as opposed to the version that you want it to be installed to (Python 2.7).
If you find any packages installed in the wrong place with 3, uninstall them with pip uninstall <packagename> and then specifically reinstall them to 2.7 with easy_install-2.7 or pip-2.7 install. If you don't see the option for easy_install-2.7 or pip-2.7, you need to install distribute and run its setup.py file with the specific version of Python you are using.
Make sure you are actually in the directory when running setup.py. For example, to install distribute, you need to cd into the appropriate directory to install.
Finally, a separate note: it's far easier to install packages with easy_install or pip, as opposed to downloading them separately. You should try doing that first. Again, distribute has more info.

Categories