My system is centos (linux). I have two Python versions, one is 2.7, the other is 3.6. I executed:
$ wget"https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb"# tar -xzvf pip-1.5.4.tar.gz
$ cd pip-1.5.4
$ python setup.py install
But, when I tried to use pip, I got an error:
File "/root/anaconda3/bin/pip", line 11, in <module>
load_entry_point('pip==1.5.4', 'console_scripts', 'pip')()
File "/root/anaconda3/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg/pkg_resources/__init__.py", line 565, in load_entry_point
File "/root/anaconda3/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg/pkg_resources/__init__.py", line 2598, in load_entry_point
File "/root/anaconda3/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg/pkg_resources/__init__.py", line 2258, in load
File "/root/anaconda3/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg/pkg_resources/__init__.py", line 2264, in resolve
File "/root/anaconda3/lib/python3.6/site-packages/pip-1.5.4-py3.6.egg/pip/__init__.py", line 9, in <module>
from pip.log import logger
File "/root/anaconda3/lib/python3.6/site-packages/pip-1.5.4-py3.6.egg/pip/log.py", line 9, in <module>
from pip._vendor import colorama, pkg_resources
File "/root/anaconda3/lib/python3.6/site-packages/pip-1.5.4-py3.6.egg/pip/_vendor/pkg_resources.py", line 1423, in <module>
register_loader_type(importlib_bootstrap.SourceFileLoader, DefaultProvider)
AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'
What can I do?
Use a virtualenv for specifying the version of python.
Virtualenvs help in solving library conflicts and it is good practice to use a different virtualEnv for every project.
Install virtualenv and virtualenvwrapper
pip install virtualenv
pip install virtualenvwrapper
Add to .zshrc or .bashrc:
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/code
source /usr/local/bin/virtualenvwrapper.sh
Specify which Python to use with mkvirtualenv:
virtualenvwrapper lets you specify the python executable without the path:
mkvirtualenv -python=python3 myVirtualEnv
Run: workon
A list of environments, empty, is printed.
Run mkvirtualenv to create a new virtualenv: mkvirtualenv temp
A new environment, temp is created and activated.
Run: workon
This time, the temp environment is included.
workon temp to work on the new virtualenv
use pip to install packages inside the virtualenv
After you're done using the virtualenv, simply deactivate out of it
(if you want to delete the virtualenv, use rmvirtualenv)
Use lssitepackages to list site-packages within the virtualenv.
Related
I'm unable to install pip using python setup.py --user, and I think my python install is generally a bit foobared.
I want to be able to use pip without sudo on my user (vagrant in this case). Everything I've read indicates that the above should work, but it just refuses to.
If I use sudo python setup.py --user then it installs correctly, but again, the sudo shouldn't be necessary because of the installation to the user directory.
python setup.py --user
Traceback (most recent call last):
File "setup.py", line 6, in <module>
from setuptools import find_packages, setup
File "/home/vagrant/.local/lib/python3.6/site-packages/setuptools/__init__.py", line 16, in <module>
import setuptools.version
File "/home/vagrant/.local/lib/python3.6/site-packages/setuptools/version.py", line 1, in <module>
import pkg_resources
File "/home/vagrant/.local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3126, in <module>
#_call_aside
File "/home/vagrant/.local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3110, in _call_aside
f(*args, **kwargs)
File "/home/vagrant/.local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 3139, in _initialize_master_working_set
working_set = WorkingSet._build_master()
File "/home/vagrant/.local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 572, in _build_master
ws = cls()
File "/home/vagrant/.local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 565, in __init__
self.add_entry(entry)
File "/home/vagrant/.local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 621, in add_entry
for dist in find_distributions(entry, True):
File "/home/vagrant/.local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 1968, in find_on_path
for dist in factory(fullpath):
File "/home/vagrant/.local/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2030, in distributions_from_metadata
if len(os.listdir(path)) == 0:
PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.6/dist-packages/websocket_client-0.54.0.dist-info'
Other bits of info...
python -V
Python 3.6.7
which python
/usr/bin/python
Also, I know that I can use a venv to achieve what I want, but the point is it should work, and doesn't, which a) bugs me to no end, and b) indicates that something is just plain wrong with my setup.
It looks to me as if you are trying to install stuff to the system Python using a non root user and hence do not have permissions to edit stuff in the system's site-packages. Please can you run a whoami and see which user you are logged in as, if you are logged in as someone other than root you should use sudo to install packages into the system's Python, e.g. sudo pip install my_package.
If you do not want to install packages to the system's Python site-packages directory then you probably should be operating in a virtualenv, this gives you a self contained Python env for your project and can be created as any user in their home directory.
To get virtualenv installed:
sudo pip install virtualenv
Then to create a new virtualenv as the user:
virtualenv my_env
Assume / Activate the virtualenv:
source my_env/bin/activate
Now you can install packages using pip as your user into your Python virtualenv:
pip install my_package
This gives you a fully separate Python env running as your user from the system Python installation. Hopefully this helps you, if I have misunderstood your use case please leave a comment and I'll update my answer accordingly.
I have a fresh install of Anaconda 4.5.10 (Python 3.6 64-bit version) on Ubuntu 16.04. I created a Python 2.7 conda env called py2.7 using the following command:
conda create -n py2.7 python=2.7
After activating the environment which pip returns /home/sam/anaconda3/envs/py2.7/bin/pip. When I try to install anything with pip I get a dependency error. For example:
(py2.7) sam#sam-M3:~$ pip install numpy
/home/sam/.local/lib/python2.7/site-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.22) or chardet (2.3.0) doesn't match a supported version!
RequestsDependencyWarning)
Traceback (most recent call last):
File "/home/sam/anaconda3/envs/py2.7/bin/pip", line 7, in <module>
from pip._internal import main
File "/usr/lib/python2.7/dist-packages/pip/__init__.py", line 18, in <module>
from pip.commands import get_summaries, get_similar_commands
File "/usr/lib/python2.7/dist-packages/pip/commands/__init__.py", line 6, in <module>
from pip.commands.completion import CompletionCommand
File "/usr/lib/python2.7/dist-packages/pip/commands/completion.py", line 4, in <module>
from pip.basecommand import Command
File "/usr/lib/python2.7/dist-packages/pip/basecommand.py", line 19, in <module>
from pip.req import InstallRequirement, parse_requirements
File "/usr/lib/python2.7/dist-packages/pip/req/__init__.py", line 3, in <module>
from .req_install import InstallRequirement
File "/usr/lib/python2.7/dist-packages/pip/req/req_install.py", line 18, in <module>
from pip._vendor.distlib.markers import interpret as markers_interpret
File "/home/sam/.local/lib/python2.7/site-packages/distlib/markers.py", line 108, in <module>
DEFAULT_CONTEXT = default_context()
File "/home/sam/.local/lib/python2.7/site-packages/distlib/markers.py", line 97, in default_context
'platform_python_implementation': platform.python_implementation(),
File "/usr/lib/python2.7/platform.py", line 1481, in python_implementation
return _sys_version()[0]
File "/usr/lib/python2.7/platform.py", line 1443, in _sys_version
repr(sys_version))
ValueError: failed to parse CPython sys.version: '2.7.15 |Anaconda, Inc.| (default, May 1 2018, 23:32:55) \n[GCC 7.2.0]'
I can see that the error is being caused by Python trying to access libraries in /usr/lib/python2.7, but I want to only access libraries that are part of the conda py2.7 env. My issue is addressed here: https://stackoverflow.com/a/46672866/10274139, but the solution isn't clear. With the conda env activated, when I list my environmental variables, I see nothing about other Python paths:
(py2.7) sam#sam-M3:~$ env | grep python
CONDA_PYTHON_EXE=/home/sam/anaconda3/bin/python
Also, if I create a Python 3.7 conda env, I get no errors when I pip install in that env.
I tried to recreate the problem. I did not encounter any errors. I am using conda version 4.5.10 (Python 3.6 64-bit version) on Ubuntu 18.04.
I ran the following commands to create my environment and install using pip.
conda create --name Test2 python=2.7
source activate Test2
pip install numpy
If you ran something similar, try updating conda and see if that helps.
To update conda :
conda update conda
I am unable to create virtual environment using both Python 2.x or Python 3.x. When I try create virtual environment I get error:
$ virtualenv my_env2 [0:05:17] ⚡[..........]
Running virtualenv with interpreter /usr/bin/python2
New python executable in /home/user/my_env2/bin/python2
Not overwriting existing python script /home/user/my_env2/bin/python (you must use /home/user/my_env2/bin/python2)
Installing setuptools, pkg_resources, pip, wheel...
Complete output from command /home/lynx/my_env2/bin/python2 - setuptools pkg_resources pip wheel:
Traceback (most recent call last):
File "<stdin>", line 7, in <module>
File "/usr/share/python-wheels/pip-9.0.1-py2.py3-none-any.whl/pip/__init__.py", line 31, in <module>
File "/usr/share/python-wheels/pip-9.0.1-py2.py3-none-any.whl/pip/vcs/mercurial.py", line 9, in <module>
File "/usr/share/python-wheels/pip-9.0.1-py2.py3-none-any.whl/pip/download.py", line 40, in <module>
ImportError: cannot import name requests
----------------------------------------
...Installing setuptools, pkg_resources, pip, wheel...done.
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/virtualenv.py", line 2375, in <module>
main()
File "/usr/lib/python3/dist-packages/virtualenv.py", line 724, in main
symlink=options.symlink)
File "/usr/lib/python3/dist-packages/virtualenv.py", line 992, in create_environment
download=download,
File "/usr/lib/python3/dist-packages/virtualenv.py", line 922, in install_wheel
call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT)
File "/usr/lib/python3/dist-packages/virtualenv.py", line 817, in call_subprocess
% (cmd_desc, proc.returncode))
OSError: Command /home/user/my_env2/bin/python2 - setuptools pkg_resources pip wheel failed with error code 1
The same error occurs for Python 3.x. Can you help me?
I had a similar problem when trying to do this with Python3. Try this command:
sudo python3 -m <your virtual env name>
First, make a directory of any name :
mkdir testing
Then, moved to this newly created directory named testing :
cd testing
When you type following command in this directory:
python3 -m venv env
You got error like :
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt install python3.8-venv
Type the following command but before that keep an eye on the version of python you installed on the machine; in my case its python3.8
sudo apt install python3.8-venv
Now, we can create a virtual environment and store its tools in the "bhandari" folder .
python3 -m venv bhandari
Note: you can named this "bhandari" folder; anyname you like( Standard practice is to name it "env" ...)
Now to activate your virtual environment, from the directory of your folder, type the following command this will activate our virtual environment in the “bhandari” folder
source bhandari/bin/activate
If you have successfully activated your virtual environment, you should see the (bhandari) word indicating that we are working in a virtual environment.
After this, we can install anything that will be isolated from the rest of the system....
I'm trying to install the pyv8 package on a virtualenv on Windows.
Here is what I did:
virtualenv venv
venv\Scripts\activate
pip install pyv8
and the last command failed with the following error:
File "<string>", line 17, in <module>
File "<proj_path>\venv\build\pyv8\setup.py", line 17, in <module>
include_dirs += os.environ["INCLUDE"].split(';')
File "<proj_path>\venv\lib\os.py", line 423, in __getitem__
return self.data[key.upper()]
KeyError: 'INCLUDE'
What am I missing?
There is actually an executable that installs it for you on windows.
When you install it, be sure to point the installation directory to your virtualenv directory. You might also want to check out this repeat
I'm trying to create an environment using virtualenv.
virtualenv test
New python executable in test/bin/python
Error [Errno 2] No such file or directory while executing command install_name_tool -change /System/Library/Fram.../Versions/2.6/Python #executable_path/../.Python test/bin/python
Could not call install_name_tool -- you must have Apple's development tools installed
Traceback (most recent call last):
File "/usr/local/bin/virtualenv", line 8, in <module>
load_entry_point('virtualenv==1.6.4', 'console_scripts', 'virtualenv')()
File "/Library/Python/2.6/site-packages/virtualenv-1.6.4-py2.6.egg/virtualenv.py", line 810, in main
never_download=options.never_download)
File "/Library/Python/2.6/site-packages/virtualenv-1.6.4-py2.6.egg/virtualenv.py", line 901, in create_environment
site_packages=site_packages, clear=clear))
File "/Library/Python/2.6/site-packages/virtualenv-1.6.4-py2.6.egg/virtualenv.py", line 1166, in install_python
py_executable])
File "/Library/Python/2.6/site-packages/virtualenv-1.6.4-py2.6.egg/virtualenv.py", line 843, in call_subprocess
cwd=cwd, env=env)
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 595, in __init__
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/subprocess.py", line 1106, in _execute_child
OSError: [Errno 2] No such file or directory
Folder is created alright, but files like activate are missing. Anyone? (I have xcode 3.2.3 installed. running python 2.6, osx 10.6.8)
Your developer tools are not installed correctly. I recommend installing Xcode 4.
I had the same error messages trying to run virtualenv on a up-to-date Max OS X Lion (10.7.2) installation with XCode installed and very up-to-date using the App Store.
I missed the /usr/bin/install_name_tool as well, however it is there in the system. Use the terminal in the following code blocks.
$ locate install_name_tool
/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/install_name_tool
/Developer/usr/bin/install_name_tool
/Developer/usr/share/man/man1/install_name_tool.1
I made a simlink from the /Developer/usr/bin to the /usr/bin with
$ sudo ln -s /Developer/usr/bin/install_name_tool /usr/bin/install_name_tool
Entering the 'which' command yields:
$ which install_name_tool
/usr/bin/install_name_tool
After that I did the virtualenv magic
$ virtualenv -p python2.6 myvirtenv
Running virtualenv with interpreter /opt/local/bin/python2.6
New python executable in myvirtenv/bin/python
Installing setuptools............................done.
Installing pip...............done.
Works like a charm now!
Use install_name_tool from #gregglind's fork of virtualenv:
git clone https://github.com/gregglind/virtualenv.git
cd virtualenv
git checkout feature/install_name_tool
sudo python setup.py install
Credits: macdhuibh (https://github.com/pypa/virtualenv/issues/7)
Get the XCode Command line tools from here:
https://developer.apple.com/downloads/index.action?name=for%20Xcode%20-#
I just fresh installed Lion, and installed XCode from the App Store - and these didn't get installed at the same time, need a separate install now I guess...