Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

virtualenv activation doesn't work

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
like image 333
enneppi Avatar asked Jan 16 '18 23:01

enneppi


People also ask

How do I activate virtualenv activation?

To activate virtualenv on Windows, first, install the pip. For this purpose, you can download and execute the latest Python installer. Next, install and create virtualenv on Windows using the pip package manager. Then, activate it using the “venvironment\Scripts\activate” command.

How do I know if virtualenv is activated?

Note: Before installing a package, look for the name of your virtual environment within parentheses just before your command prompt. In the example above, the name of the environment is venv . If the name shows up, then you know that your virtual environment is active, and you can install your external dependencies.


2 Answers

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

like image 70
scottapotamus Avatar answered Oct 06 '22 07:10

scottapotamus


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

like image 32
kmario23 Avatar answered Oct 06 '22 07:10

kmario23