How to set python version for pip in virtualenv? - python

Need to run some project that require installing packages available only for Python 2.7 (I can't change packages). I'm running Ubuntu 17.04 with Python 3.5.3 . I created virtual environment by issuing next command:
sudo virtualenv --python=/usr/bin/python2.7 env
Seems like I have version I need:
(env) serv#serv:/var/m$ python -V
Python 2.7.13
But when I'm trying to install packages(those only for Python 2.7) it DOESN'T look like my virtualenv is using python 2.7 what is more interested, it refers to already installed packages(I supposed that virtualenv won't have any packages,except for some default as pip).
Installing mysql-python (this usually error occurs only when installing from python 3):
ImportError: No module named 'ConfigParser'
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-fi75i_pl/mysql-python/
When I'm installing package for Python3 - SQLAlchemy:
Requirement already satisfied: SQLAlchemy in /home/loraserver/.local/lib/python3.5/site-packages
What can I do to run my code from Python 2.7 environment?
EDIT 1: I'm installing mysql-python by:
pip install mysql-python
This gives me Permission denied.
When I'm trying :
sudo pip install mysql-python
It gives me error the same from installing python on python3.

I think the issue is that you created your virtual environment using sudo.
This may require you to use sudo whenever working inside of your environment, causing your packages to be installed globally.
Try creating a new virtual environment without using sudo and then installing your packages from there.
Run the following commands to test it:
virtualenv --python=/usr/bin/python2.7 testenv
source testenv/bin/activate
pip install mysql-python

Do sudo rm -rf env and then (without using sudo) start over:
$ virtualenv venv
$ source venv/bin/activate
Verify that which python shows a python within your project, and similarly for which pip. Then:
$ pip install mysql-python
BTW, there is a nice program named /usr/bin/env, so it is usual to name your directory "venv" rather than "env".

Related

Mac M1 python packages error after upgrade to python3

I am writing a project in NativeScript and I received the following error the last few days when I tried the commands: ns run ios or ns doctor.
Couldn't retrieve installed python packages.
The Python 'six' package not found.
I tried python and pip upgrade and also the command pip install six.
Nothing of them fixed the problem.
I believe that is not a NativeScript issue, is about the configuration of the python packages in my machine. I mention that I am using a MacBook with M1 chip and it is running the 12.5 OS version.
I will appreciate any suggestions on this situation.
Lastly, I found the solution. It was about the python folder into the path /usr/local/bin/python
You could check it by the following command: where python
In my case this folder is missing, perhaps I deleted it after the upgrade of the python3.
That was a mistake both folders should exist on this path!
If you type: where python you should receive: /usr/local/bin/python
If you type: where python3 you should receive: /usr/local/bin/python3
In order to fix the error, I installed python again by using the brew install pyenv
this suggestion helps me to install it properly.
In the end, in order to eliminate all errors I installed the Python six package by using the command:
pip install --ignore-installed six
Try doing it with the python virtual environment. Following are the steps.
Create a virtual environment.
Activate the virtual environment.
Run the pip install command with the virtual environment active.
Implement as follows:
use correct version of Python when creating VENV
python3 -m venv venv
activate on Unix or MacOS
source venv/bin/activate
activate on Windows (cmd.exe)
venv\Scripts\activate.bat
activate on Windows (PowerShell)
'venv\Scripts\Activate.ps1'
install the required package in the virtual environment
python3 -m pip install --upgrade pip
python3 -m pip install six
Note: This works only when you are in the virtual environment.

How to fix "module 'platform' has no attribute 'linux_distribution'" when installing new packages with Python3.8?

I had Python versions of 2.7 and 3.5. I wanted the install a newer version of Python which is python 3.8. I am using Ubuntu 16.04 and I can not just uninstall Python 3.5 due to the dependencies. So in order to run my scripts, I use python3.8 app.py. No problem so far. But when I want to install new packages via pip:
python3.8 -m pip install pylint
It throws an error:
AttributeError: module 'platform' has no attribute 'linux_distribution'
So far, I tried:
sudo update-alternatives --config python3
and chose python3.8 and run command by starting with python3 but no luck.
Then:
sudo ln -sf /usr/bin/python3.5 /usr/bin/python3
I also tried running the command by starting with python3 but it did not work either.
How can I fix it so that I can install new packages to my new version of Python?
It looks like at least on my Ubuntu 16.04, pip is shared for all Python versions in /usr/lib/python3/dist-packages/pip.
This is what I did to get it working again:
sudo apt remove python3-pip
sudo python3.8 -m easy_install pip
You might want to install the python 3.5 version of pip again with sudo python3.5 -m easy_install pip.
Python 3.8 removed some stuff. I solved my problems with pip (specifically pip install) by installing pip with curl.
What worked for me was downloading get-pip.py and run it with Python 3.8:
cd ~/Downloads
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3.8 get-pip.py
Source: https://pip.pypa.io/en/stable/installing/
The problem is that package.linux_distribution was deprecated starting with Python 3.5(?). and removed altogether for Python 3.8.
Use the distro package instead. This package only works on Linux however.
I ran into this problem after installing OpenCobolIDE on Linux Mint 20, having upgraded Python to the latest level. have submitted a code fix to the OpenCobolIDE author to review and test. I was able to get the IDE to start up and run with this fix.
Essentially the fix uses the distro package if available, otherwise it uses the old platform package. For example:
This code imports distro if available:
import platform
using_distro = False
try:
import distro
using_distro = True
except ImportError:
pass
Then you can test the value of using_distro to determine whether to get the linux distro type from package or distro, for example:
if using_distro:
linux_distro = distro.like()
else:
linux_distro = platform.linux_distribution()[0]
In my case, removing python-pip-whl package helped:
apt-get remove python-pip-whl
It removed also pip and virtualenv, so I had to install them again:
curl https://bootstrap.pypa.io/get-pip.py | python3
pip install virtualenv
Check if your wheels installation is old. I was getting this same error and fixed it with
python3.8 -m pip install --upgrade pip setuptools wheel
Pylint seems to work on python3.8
I recently had this error and it turns out that I had a package called platform at a folder on my path ahead of the standard library and so the interpreter imported that instead. Check your path to what it is that you're actually importing.
If you have this issue when running a docker-compose up command. The solutions above do not work. You should install docker ce (https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04)

Command "python3 -m pip install [...]" failed with error code 1 in None

I am trying to install the PyInstaller module for python through the terminal on my mac, and it is not working.
I am trying to install this module so I can package a program. I have tried to install through terminal through different commands (e.g. "pip install pyinstaller), but it is not working, even though I know I have PIP correctly installed.
When trying to install, I type, python3 -m pip install pyinstaller.
The result is Command "/usr/local/bin/python3 -m pip install --ignore-installed --no-user --prefix /private/var/folders/w5/rlzrygt57j3c3nl4p29d2djc0000gw/T/pip-build-env-mjrd7sir https://files.pythonhosted.org/packages/b2/86/095d2f7829badc207c893dd4ac767e871f6cd547145df797ea26baea4e2e/setuptools-41.2.0-py2.py3-none-any.whl#sha256=4380abcf2a4ffd1a5ba22d687c6d690dce83b2b51c70e9c6d09f7e8c7e8040dc https://files.pythonhosted.org/packages/00/83/b4a77d044e78ad1a45610eb88f745be2fd2c6d658f9798a15e384b7d57c9/wheel-0.33.6-py2.py3-none-any.whl#sha256=f4da1763d3becf2e2cd92a14a7c920f0f00eca30fdde9ea992c836685b9faf28" failed with error code 1 in None
I have upgraded PIP, and tried using sudo python3 -m pip install pyinstaller, but I am not in the sudoers file.
Please help
Create a virtual environment for your project in your project directory.
python3 -m venv env
source env/bin/activate
Test which python is activated
which python
This will show that your local python in the virtualenv you just created is now the current python
Try installing now using
pip install pyinstaller
This ought to work for you. Always use virtual environments for your projects instead of installing system wide libraries unless you really need to set this up for the system.

Error with installing textgenrnn

I`m trying to install library called textgenrnn, followed by this in docs:
pip3 install textgenrnn
Got error that my Python version is 2.7:
Collecting textgenrnn
Using cached https://files.pythonhosted.org/packages/00/69/5d995322502f8a33d408c547a6dbf00e74d4434ecc1b704b684260739b21/textgenrnn-1.3.1.tar.gz
textgenrnn requires Python '>=3' but the running Python is 2.7.15
You are using pip version 9.0.3, however version 18.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
I also check my Python version using this command:
python -V
and get
Python 3.6.5 :: Anaconda, Inc.
Update: After uninstalling anaconda I got this:
dmitriy#dmitriy-PC:~$ python -V
Python 2.7.15rc1
dmitriy#dmitriy-PC:~$ python3 -V
Python 3.6.5
My first think is to delete or disable python 2. But after googling I decided that python2 is impossible to uninstall or disable
For making python3 as your default by setting the alias command or you can configure the .bashrc file for conda and set the path for conda
alias python='python3' #create python3 as your default
get install via pip
Another Method:
create a environment after you install conda and you will get a python3 environment. From there you can install via pip
Either of the method should work here.
Just install library from source.
wget https://files.pythonhosted.org/packages/00/69/5d995322502f8a33d408c547a6dbf00e74d4434ecc1b704b684260739b21/textgenrnn-1.3.1.tar.gz
tar -xvzf textgenrnn*.tar.gz
cd textgenrnn-1.3.1
sudo python3 setup.py install
That`s all!

Installing python packages with multiple versions on OSX

I am attempting to install a package for python3.4 on Mac OSX 10.9.4. As you know, python ships with OSX, so when I installed python3.4 I was happy to find that it came with its own version of pip, that would install packages to it (installing pip on a mac with multiple versions of python will cause it to install on the system's python2.7.)
I had previously tried installing this package (https://pypi.python.org/pypi/chrome/0.0.1) with my first installation of pip (the one tied to python2.7) and found that it successfully installed on that version, but not on any others.
I ran an install with the new pip keyword for python3.4 (which when called by itself spits out the help page so i know it works) and it told me that the package was already installed and to try updating. The update revealed that I already had the most recent version. so I tried uninstalling it from just the python3.4 and reinstalling to no avail, and got the same results when uninstalling pip from python2.7 and reinstalling only on version 3.4.
I know that's a bit hard to follow but hopefully that makes sense.
I also reviewed the content here with no success.
RESOLVED:
while python did have a directory named the same as a directory it uses with packages, this was not the correct directory, for me it was in a subdirectory of library. while documentation said that referencing pip2 would cause the package to install on python3.4, this was false. however, referencing pip3.4 worked for me.
My suggestion is that you start using virtualenv.
Assuming you have 3.4 installed, then you should also have pyvenv. As for pip and 3.4, it should already be installed.
Using for example version 3.4 create your own virtual environment and activate it:
$ mkdir ~/venv
$ pyvenv-3.4 ~/venv/py34
$ source ~/venv/py34/bin/activate
$ deactive # does what is says...
$ source ~/venv/py34/bin/activate
$ pip install ... # whatever package you need
With version 2.7 first install virtualenv and then create your own virtual environment and activate it. Make sure that setuptools and pip are updated:
$ virtualenv-2.7 ~/venv/venv27
$ . ~/venv/venv27/bin/activate
$ pip install -U setuptools
$ pip install -U pip
$ pip install ... # whatever package you need

Categories