11

I've created a virtual environment with:

$ virtualenv my_ven_test

then let's activate the environment with:

$ source my_ven_test/bin/activate

now let's install a package:

(my_ven_test) $ pip install mysql-connector==2.1.3

This last line does not take effect. In fact if I check:

(my_ven_test) $ pip freeze

I see no package installed (as well as the my_ven_test/lib/python/site-package directory doesn't contain the mysql-connector package)

Could you guide me in solving this issue?

Some notes:

  • python version: 2.7
  • virtualenv version: 15.1.0
enneppi
  • 1,029
  • 2
  • 15
  • 33
  • 2
    this is `(my_ven_test) $ pip freeze` right? – j-i-l Jan 16 '18 at 23:45
  • yes, it is. i've modified the post. thanks – enneppi Jan 16 '18 at 23:47
  • 2
    Did you move/rename your venv after creating it? If so, [this question](https://stackoverflow.com/questions/6628476/renaming-a-virtualenv-folder-without-breaking-it) may shed some light on the issue. May be useful to check that the output of `which pip` points to the `pip` executable in the virtualenv directory. If not, you may have issues with your PATH enviroment variable. – sytech Jan 16 '18 at 23:47
  • 1
    Whats the output of `echo $PATH`, from virtualenv? – heemayl Jan 16 '18 at 23:48
  • Did you already upgraded pip with `pip install --upgrade pip`? – abautista Jan 16 '18 at 23:49
  • @AlejandroBR yes requirements already up-to-date – enneppi Jan 16 '18 at 23:52
  • @heemayl this is the output /home/hadoop/my_ven_test/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/aws/bin – enneppi Jan 16 '18 at 23:53
  • is the path in the first line of `my_ven_test/bin/pip` pointing to the correct python? then, is the path in `my_ven_test/bin/activate` line 42 or so the correct path to your virtualenv? if you moved your virtualenv, move it back to where you created it and run `virtualenv --relocatable my_ven_test`, then you can move it again. – j-i-l Jan 16 '18 at 23:54
  • @jojo yes to both. I didn't move the virtualenv – enneppi Jan 16 '18 at 23:58
  • 3
    PATH looks OK to me... Is it possible that you installed pip *after* creating the virtualenv? Check that the `pip` executable is actually present in the venv's `/bin` directory. If it's in there, then the output of `which pip` should be like `/home/hadoop/my_ven_test/bin/pip` I would recommend, for sanity's sake, trying to create another virtualenv and see if you're still getting the same problem. Keep in mind you don't want to rename/move your venv after creating it. – sytech Jan 16 '18 at 23:59
  • @sytech already checked. all things seems ok (there was no installation after the creation of virtualenv) – enneppi Jan 17 '18 at 00:01
  • did `pip install mysql-connector==2.1.3` execute without errors? when I run it in a new venv it fails. – j-i-l Jan 17 '18 at 00:05
  • @jojo executed without errors. keep in mind that this is only a test. you can use pip install numpy as well – enneppi Jan 17 '18 at 00:06
  • :P i ran install numpy first with expected behavior and showing up in a `freeze`. To me there is no flaw apparent in your setup, thus my question. – j-i-l Jan 17 '18 at 00:08
  • I always just run `python -m pip`, since I typically have numerous Python installs and virtualenvs. `pip` should work out of the box, but manually specifying the Python interpreter should **always** work. – Alex Huszagh Jan 17 '18 at 00:17
  • 1
    @AlexanderHuszagh no difference with python -m pip – enneppi Jan 17 '18 at 00:25
  • 1
    `mysql-connector` doesn't create `mysql-connector` in `site-packages`, it creates `mysql` and `mysqlx` directories plus `_mysql_connector.so` and `_mysqlxpb.so` libraries. Try `pip show mysql-connector`. – phd Jan 17 '18 at 10:52
  • @phd I have no output with `pip show mysql-connector`. Same thing if I type `pip install pandas`. `mysql-connector` is just an example – enneppi Jan 17 '18 at 11:03
  • 2
    Someone suggested looking at the output of `which pip`, and I don't see that here. After you activate your venv, what do you get from `which pip`, `echo $VIRTUAL_ENV`, and `pwd`? – Nathan Vērzemnieks Jan 20 '18 at 22:18
  • Oh, here's another question: does this happen with any virtual environment you create, or is it just this one? – Nathan Vērzemnieks Jan 20 '18 at 22:30
  • I would also request output for `pip -V` and `python -V` inside the venv. Not enough info provided in question. Also, a log from `pip install somepkg -vvv` is often helpful. – hoefling Jan 21 '18 at 14:31
  • Please provide console outpur of `pip install mysql-connector==2.1.3` – sureshvv Jan 24 '18 at 03:07

4 Answers4

8

Forget about virtualenv, use the brand new Pipenv which is recommended by Python.org


Pipenv automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile (more about this below) as you install/uninstall packages.


First install pipenv using:

$ pip install pipenv

Then, for installing project specific packages, first create your project folder and then install all necessary packages for your project like:

$ mkdir myproject
$ cd myproject

# install `requests` library
$ pipenv install requests

# install more libraries required for your project
$ pipenv install mysql-connector
$ pipenv install numpy

This will create two files, namely Pipfile and Pipfile.lock. You can find the list of all installed packages for the current project in the file Pipfile while Pipfile.lock has info on hashes like sha256 for all the installed packages and their dependencies.


Once you're done with the installation of all necessary packages for your project, then do:

$ pipenv shell

which will launch a subshell in virtual environment. (This does the similar job of source /your/virtualenv/activate)

Then you can start coding.. For example, you can first test whether installed packages are working fine by launching a Python shell and import the packages like below:

$ python
>>> import requests
# ....

To exit out of the (virtualenv) shell, simply do:

$ exit

Now, you're out of the virtual environment created by pipenv

Read more about it installing packages for your project @ pipenv.kennethreitz.org

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
kmario23
  • 57,311
  • 13
  • 161
  • 150
  • I found this intriguing. Unfortunatly, with any (new or existing) project I get this - `pip.exceptions.InstallationError: Command "python setup.py egg_info" failed with error code 1 in /tmp/tmp13ba53__build/mysqlclient/`. What paths exactly is this looking for requirements.txt? It says it finds requirements.txt even in a completely empty folder. – addohm Jan 24 '18 at 02:40
  • @Jaberwocky Sure!! try checking it with `pipenv lock -r` to get a requirements.txt file. – kmario23 Jan 24 '18 at 02:47
  • I don't want one. I want to prevent `pipenv install` from "automatically detecting" one. It's seeing a requirements.txt when run from an empty folder. – addohm Jan 24 '18 at 02:49
  • @Jaberwocky that's strange. Maybe you can report the issue here: https://github.com/pypa/pipenv/issues – kmario23 Jan 24 '18 at 03:06
1

When you are within a venv you should use the following to install a package:

py -m pip install mysql-connector==2.1.3

the -m ensures the package is installed into your venv and not into your root python

scottapotamus
  • 548
  • 3
  • 18
0

Try installing the package without activating virtualenv:

# Install it
my_ven_test/bin/pip install mysql-connector==2.1.3
# Use grep to check if exists
my_ven_test/bin/pip list | grep mysql-connector

If that works, then try to activate virtualenv by running this code:

. my_ven_test/bin/activate

Try installing another package

pip install flake8

Afterwards, search for those two packages

pip list | grep mysql-connector
pip list | grep flake8

Let me know the result.

b4oshany
  • 692
  • 1
  • 7
  • 15
-1

after jumping to Scripts folder cd Scripts just write .\activate It works perfectly see here on youtube

Ipythonate
  • 19
  • 5