virtualenv activation doesn't work
Solution 1
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
Solution 2
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
Related videos on Youtube
Comments
-
enneppi over 1 year
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
-
jojo over 6 yearsthis is
(my_ven_test) $ pip freeze
right? -
sytech over 6 yearsDid you move/rename your venv after creating it? If so, this question may shed some light on the issue. May be useful to check that the output of
which pip
points to thepip
executable in the virtualenv directory. If not, you may have issues with your PATH enviroment variable. -
sytech over 6 yearsPATH 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 ofwhich 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. -
Nathan Vērzemnieks over 6 yearsSomeone suggested looking at the output of
which pip
, and I don't see that here. After you activate your venv, what do you get fromwhich pip
,echo $VIRTUAL_ENV
, andpwd
? -
enneppi@AlexanderHuszagh no difference with python -m pip
-
phd
mysql-connector
doesn't createmysql-connector
insite-packages
, it createsmysql
andmysqlx
directories plus_mysql_connector.so
and_mysqlxpb.so
libraries. Trypip show mysql-connector
. -
heemaylWhats the output of
echo $PATH
, from virtualenv?
-
addohm over 6 yearsI 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. -
kmario23 over 6 years@Jaberwocky Sure!! try checking it with
pipenv lock -r
to get a requirements.txt file. -
addohm over 6 yearsI 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. -
kmario23 over 6 years@Jaberwocky that's strange. Maybe you can report the issue here: github.com/pypa/pipenv/issues