I was trying to install the Adafruit_Python_MPR121 Library in my venv folder, but it always installed it into the global dist-packages and I cannot access them from my venv.
I cannot use --site-packages because I need some local packages in the env.
Does someone know a solution for it?
I'm sure you can and should use --site-packages. It doesn't do what you seem to think it does — it doesn't make pip install all packages globally. It makes python in the virtual env access global site-packages but pip still install packages in the virtual env (after you activate it with . env/bin/activate).
Related
I have been through numerous similar questions and I still don't understand how are we activating virtual environment for Django projects. Please mention explanation for how each command works and one more questions is that why do we not need to install python in Django's virtual environment, I am getting confused. Thank you in advance, please help this newbie.
Benefits
You can use any version of python you want for a specific environment
without having to worry about collisions (shoutout to my python 2.7
mac users!)
You can organize your packages much better and know exactly the
packages you need to run your code incase someone else needs to run
it on their machine
Your main python package directory does not get FLOODED with
unnecessary python packages
To create virtual environment
step 1 install environment package (virtualenv) using pip
pip install virtualenv
step 2 create virtualenv
virtualenv env_name #<- env_name is virtualenv name you can set any
step 3 Activate Virtual env
env_name\Scripts\activate #<- for window
step 4 Install pakages you wan to install in virtual env
cmd(env_name): pip install django
Note that python is install in your virtual env automaticaty the
version is same as in your local machine
There is no difference between activating virtual environment for Django or for other purposes. Django on it's own does not differ from any Python library out there.
Python virtual environment allows you to separate your system Python and it's libraries and create self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.
On Linux, assuming you already have Python 3 and pip3 installed:
# install virtualenv package (skip if you have it already)
pip3 install virtualenv
# create virtual environment in directory "tutorial-env"
python3 -m venv tutorial-env
# activate virtual environment
source tutorial-env/bin/activate
Upon activation below command should give path to new Python binary:
which python3
Similarly with pip3
which pip3
As long as your environment is activate you can run pip3 install $package_name and it will install it inside virtual environment.
To deactivate your virtual environment:
deactivate
For more info and commands for Windows:
https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/
https://docs.python.org/3/tutorial/venv.html
Lately I've been using conda instead of virtualenv and I think I like it better, except when I install packages I don't think they are only installed into my virtual environment.
If I run pip freeze within my virtual environment I get the list of packages installed, no surprise there. However, when I deactivate the virtual environment and run pip freeze again, I get the same list. Is my environment not separated from my base distro? Plus, how do I create a clean environment if my packages are shared across all environments?
Note: I noticed this after installing packages with pip using pip setup.py install within a package. However, according to the docs you can use pip in a conda virtual environment.
I want to do some experiments with tensorflow in virtualenv. I followed the tutorial and install tensorflow and virtualenv. Everything works well. However, when I try to use matplotlib.pylot. It said I need framework. So I follow the [solution][1] https://matplotlib.org/faq/osx_framework.html. It said "If you are on Python 3, use venv instead of virtualenv"
python -m venv my-virtualenv
source my-virtualenv/bin/activate
Then I activate the virtual Environments, but all the module that I install in virtualenv cannot find. I think I just use venv, but not use virtualenv. Now I want to use virtualenv, and don't want to use venv any more. I even try to delete venv, but it seems it doesn't work. What should I do? How do I what virtual environments I am using? Thank you.
You've to install packages inside the virtual environment for them to be available to used when the virtual environment is activated. So, install the packages after activating the environment!
I've created a Python virtual environment, and activate it by doing:
joe#joe-mint $ source ./venvs/deep-learning/bin/activate
Which turns the prompt into:
(deep-learning) joe#joe-mint $
Now whenever I run a python package or try to install one, the system seems to ignore the fact that it's in a virtual environment and does things system-wide:
(deep-learning) joe#joe-mint $ which pip
/usr/local/bin/pip
The same happens when I try to install new packages that aren't on my system; it installs them to the system files (i.e. /usr/bin) instead of the virtual environment.
What's wrong with my virtual environment? How do I get it to ignore system files and do everything inside the environment?
I've looked at this question which says to use an explicit flag when creating the virtual environment to make it use the local environment packages, but I used python-3.5 -m venv to create the virtual environment, and this flag is removed in this version as it's now a default option.
I've also looked at this question and can confirm that the VIRTUAL_ENV variable is set correctly in the activate file of the virtual environment.
Here was the problem:
It seems that if you run pip on a venv without a local pip installation, then it will default to the system's pip outside the venv. Even if you've activated a virtual environment, this seems to want to install packages on the system rather than in the venv.
Here was the solution:
First, I had to install the virtual environment without pip due to a bug that has long remained unresolved.
Second, I installed pip in the virtual environment as per the instruction here. However, doing so required using some temporary folders that for some reason my user didn't have access to. So this failed, and the only way I could get it to work was to become root.
sudo su
activate ..../venvs/deep-learning/bin/activate to activate the virtual environment.
curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | python as per the answer linked above.
Although which pip now indicated the correct pip (inside the venv) was being used, running pip would use the system one! Deactivating (deactivate) and reactivating the venv solved this.
Now it took me a while to realise that having installed this as root caused me permission errors when trying to install more packages using pip inside the virtual environment.
chown <user>:<group> -R ..../venvs/deep-learning/*
And that was it. After these steps, I could activate the venv and run pip correctly. It would use the pip inside the venv, and install packages inside the venv.
I'm using virtualenv for sandboxing my Python environment, pip to install/uninstall packages and yolk to list the packages.
I can install packages to my virtual environment by using pip install <package name> -e=<environment name> and I guess I don't need to have pip inside my virtual environment. Am i correct?
If I need to list out all the installed packages in my virtual environment, can I use yolk -l to do so? I know I can do this by keeping yolk installed inside the environment but is this also possible by keeping yolk outside the environment i.e. the global Python installation.
Thanks.
Here is your workflow:
Add virtualenv and pip to your global environment.
Create virtualenvs
Within a virtualenv, add new packages
I recommend you look into virtualenvwrapper. It makes the maintenance of virtualenvs way easier.
Download and install virtualenvwrapper in your global environment
Create directory ~/.virtualenvs
Modify your ~/.bashrc with these statements:
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages --python=python2.6'
source /usr/local/bin/virtualenvwrapper.sh
Then you can create, delete, modify, and change between virtualenvs easily.
So, for your questions:
Should I put pip inside my virtualenv?
No, do not do that.
Should I use yolk to list the packages?
Not familiar with yolk. I just use pip freeze and then I get a
requirements file that lists all the packages for recreating my
environment.