I use Python and pip trhough the asdf version manager, and in my older PC, when I create a new venv it already came with some default packages. I'm now using a newer verison of python (3.9.12) and pip (22.0.4), and it does not come with basic and essential things, such as the gnureadline, to be able to see the commands history in the python's shell. It's really annoying having to install it again in all new venvs and I'm trying to avoid an alias to create a venv and install what I want inside it. There is a way to set some default packages to all venvs?
There is an option:
--system-site-packages
Give the virtual environment access to the system
site-packages dir.
So if you use python3 -m venv --system-site-packages .venv then you may install the packages you want available to all envs at the system level. You'll have to be hygienic about the packages installed at the system level.
If the system python on your distribution is a hot mess, and you can't remove things you don't want visible in venvs from it, then you'll want to look for another option.
One possibility is just to install common packages to some target directory:
python3 -m pip install dep1 dep2 --target=/path/to/common
And then make this directory of packages always visible:
export PYTHONPATH=/path/to/common
Related
I am trying to install a Python package with this command
pip install <name of package>
I'm getting permission errors and I'm not sure why. I could run it with sudo, but someone told me that was a bad idea, and I should use a virtualenv instead.
What is a virtualenv? What does it do for me?
Running with the system Python and libraries limits you to one specific Python version, chosen by your OS provider. Trying to run all Python applications on one Python installation makes it likely that version conflicts will occur among the collection of libraries. It's also possible that changes to the system Python will break other OS features that depend on it.
Virtual environments, or "virtualenvs" are lightweight, self-contained Python installations, designed to be set up with a minimum of fuss, and to "just work" without requiring extensive configuration or specialized knowledge.
virtualenv avoids the need to install Python packages globally. When a virtualenv is active, pip will install packages within the environment, which does not affect the base Python installation in any way.
In Python 3.3 or later, you can create a virtualenv as follows:
$ python3 -m venv ENV_DIR
For Windows, you should replace python3 with the full path to python.exe:
>C:\Python34\python.exe -m venv ENV_DIR
(This is a typical Python installation; your system may vary.)
In older versions of Python, including Python 2, one of the following commands should work in most cases:
$ virtualenv ENV_DIR
$ venv ENV_DIR
$ pyvenv ENV_DIR
$ pyvenv3 ENV_DIR
ENV_DIR should be a non-existent directory. The directory can have any name, but to keep these instructions simple, I will assume you have created your virtualenv in a directory called venv (e.g. with python3 -m venv ./venv).
To work in your virtualenv, you activate it:
$ . ./venv/bin/activate
(venv)$
Or use this if you have a windows system:
$ venv\Scripts\activate
The (venv) in the shell prompt lets you know which virtualenv you have activated, but you can turn this feature off if you do not like it. You can run all the usual Python commands, and they will be local to your virtualenv:
(venv)$ pip install requests numpy
[...]
(venv)$ python
[...]
>>> import requests
>>> import numpy as np
>>>
python will run the version of Python that you installed into your virtualenv, so (for example) you don't have to type python3 to get Python 3. The Python that it runs will have access to all the standard library modules and all the packages you installed into the virtualenv, but (by default) none of the packages installed in the system-wide site-packages directory.
This last rule is important: by restricting your virtualenv to only use locally-installed packages, you can ensure that you control exactly which dependencies your project is using, even if some new system-wide package gets installed or updated next week. If you like, you can get a listing of your installed packages:
(venv)$ pip freeze
requests==2.13.0
numpy==1.12.0
(venv)$
pip can also parse this format and install from it, and it will install the same versions, even if updates have been released in the meantime:
(venv)$ pip freeze >requirements.txt
(some-other-venv)$ pip install -r requirements.txt
[...]
(some-other-venv)$ python
>>> import requests
>>> import numpy as np
>>>
You can get out of the virtualenv by deactivating it:
(venv)$ deactivate
$ python
[...]
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'requests'
You can create as many virtualenvs as you like, and they won't interfere with each other, nor with your system packages. A virtualenv is "just" a directory with a bunch of binaries and scripts under it, so you can remove a virtualenv the same way you remove any directory (rm -r venv on Unix). If the virtualenv is activated when you remove it, you may confuse your shell, so it's probably a good idea to deactivate first in that case.
Some times you are not given root privileges and you might end up not being able to use sudo. Many other times, it's not advisable to use sudo to install packages as it might overwrite some package which might be in use by some other applications.
Virtualenv can help you create a separate environment where you don't need root privileges as well as be able to tailor the environment according to your need. It consists of self-contained python installation which only interacts with your specific created environment.
So basically, it gives you a bit of freedom as well as avoid damaging (or modifying) the root environment which might be hosting many old functionalities of old applications.
Installation is pretty easy too.
Installing packages with sudo pip will install packages globally, which may break some system tools.
By install globally it means you will install your packages in place like /usr/lib/python2.7/site-package so if some packages need a previous version of your python packages, this action may break it.
virtualenv allows you to avoid installing Python packages globally by making an isolated python environment. That means it will install packages just in your desire project folder.
On mac and linux
Install
python3 -m pip install --user virtualenv
Creating a Virtual Env: Go to your desired project folder
python3 -m virtualenv env
Activating a virtualenv: In your desired project folder
source env/bin/activate
After activating you can install your packages using pip.
For more information about using it in Windows:
How to use virtualenv in Windows
I am going to break your question into two parts.
What is a virtualenv?
Python has its own way of downloading, storing, and resolving site packages. But Python can not differentiate between different versions in the site-package directory. Packages will be installed in one of the directories, whose name can be found by running the site.getsitepackages() commands.
>>> import site
>>> site.getsitepackages()
This means package_v2.0.1 and package_v3.0.1 have to be in the same directory with the same name i.e. package, which is obviously not possible. Now, you may ask why we would need the same package with different versions on our system. This is because multiple projects may require different versions of Python packages or even different Python versions themselves. So there was a need to have something which will resolve these conflicts and Virtualenv came into the picture to solve this issue.
What does it do for me?
It isolates the environment for Python projects so that each project can have its own dependencies.
I need python3.6 for tensorflow installation, so I downloaded python3.6.12.tar. And I found that I should pip install tarfile. However, in this case it is an older version of python. FYI, In my computer(laptop) I installed python3.9.
My question is: can I pip install python.tar inside a virtualenv?
This is not how virtual environments work. I suggest you to do a little bit more research on virtual environments in Python.
Virtual Environments and Packages
Basically you need to install the necessary python version onto your machine. Then go ahead and use that specific python (which is version 3.6 in your case), to create a virtual environment with the command
~ /usr/bin/<path-to-python3.6> -m venv venv
This command will create a folder called venv. Now you need to source the activation script inside this folder to activate your environment.
Handy note: if you are dealing with different versions of python, a more robust way of handling such situations is via using a tool called pyenv.
Running command line:
virtualenv --system-site-packages venv
I'm expecting venv folder venv\Lib\site-packages to contain all the necessary library from the projects that are located in:
C:\Users\XXX\AppData\Local\Programs\Python\Python36\Lib\site-packages\
But it's not the case, only a few are installed.
Example, my program currently use pdfminer which is in
C:\Users\XXXX\AppData\Local\Programs\Python\Python36\Lib\site-packages\
I want it to be included in venv\Lib\site-packages but it is not copied.
Any advice?
--system-site-packages doesn't copy packages, it just allows python from the virtualenv to access packages in C:\Users\XXX\AppData\Local\Programs\Python\Python36\Lib\site-packages\.
There is no way to copy packages because they could depend on their installation directory. If you want these packages in the virtualenv don't use --system-site-packages and install all packages in the virtualenv.
A virtualenv environment is the same as if you have just installed a new version of Python. It has no packages other than the standard packages provided with Python. If you want other packages, you have to install them with 'pip' or however you'd do it with the native Python version that you are using.
So in general, just do pip install <packagename>.
If you find yourself often wanting to create virtualenvs with a standard set of base packages, then put together a requirements.txt file listing all of the packages you want to install as a base, and do pip install -r requirements.txt inside a new virtualenv, right after you create it.
One nice thing about a virtualenv is that it's all yours. Your user owns 100% of it, unlike the base Python version that is owned by the system. To install new packages into the base Python version, you often have to have root access (sudo privileges). With virtualenvs, you don't need special permissions (in fact, you'll get all screwed up if you use sudo in a virtualenv) to install all the packages you want. Everything you do lives within your own home directory. Another neat thing is that when you are done with a virtualenv, you just throw away the root directory that contains it.
If its not mandatory to use virtualenv, I would suggest to go with Anaconda. That'll pretty much help your concern.
Conda as a package manager helps you find and install packages. By default quite a few packages are already installed, so as to set you up quickly for your project. To check the list of packages installed in terminal, type: conda list to obtain the packages installed using conda.
If you need a package that requires a different version of Python, you do not need to switch to a different environment manager, because conda is also an environment manager.
With just a few commands, you can set up a totally separate environment to run that different version of Python, while continuing to run your usual version of Python in your normal environment
I use SublimeText for programming in python - it's easy and to build a program I have to just press Ctrl+b. To use additional libraries such as "requests" or "tensorflow" I've installed "VirtualEnv" package for SublimeText https://packagecontrol.io/packages/Virtualenv. I tried to use source /... my path.../activate, but instead of activation there is different python versions
suleyman#Linuxoid:~/.virtualenvs/bin$ ls
python python3 python3.6
And i can't use pip install bs4 (for example) to install additional python's libraries. So, how to install python's libraries in SublimeText's "virtual environment" package? Thank you
SublimeText itself doesn't install Python packages. Instead, the virtualenv package substitutes a Python binary that you specify instead of the system installation whenever you hit Ctrl+B. Your virtual environments are stored in the ~/.virtualenvs directory (though you can keep them anywhere). From your snippet above, it appears you have three virtualenvs installed, called python, python3 and python3.6.
To install TensorFlow or Requests, you need to activate your virtualenv from the terminal:
source ~/.virtualenvs/python3.6/bin/activate
Then run your installation commands:
pip install tensorflow
You can verify the installation completed successfully by running pip freeze.
In sublime, check the virtualenv package settings to confirm that it's pointed at your ~/.virtualenvs directory. When you run the command to select a virtualenv (Ctrl+Shift+P), you will see a list of all the virtualenvs saved to that folder. If you select the python3.6 environment, you can now use Tensorflow.
I am trying to install a Python package with this command
pip install <name of package>
I'm getting permission errors and I'm not sure why. I could run it with sudo, but someone told me that was a bad idea, and I should use a virtualenv instead.
What is a virtualenv? What does it do for me?
Running with the system Python and libraries limits you to one specific Python version, chosen by your OS provider. Trying to run all Python applications on one Python installation makes it likely that version conflicts will occur among the collection of libraries. It's also possible that changes to the system Python will break other OS features that depend on it.
Virtual environments, or "virtualenvs" are lightweight, self-contained Python installations, designed to be set up with a minimum of fuss, and to "just work" without requiring extensive configuration or specialized knowledge.
virtualenv avoids the need to install Python packages globally. When a virtualenv is active, pip will install packages within the environment, which does not affect the base Python installation in any way.
In Python 3.3 or later, you can create a virtualenv as follows:
$ python3 -m venv ENV_DIR
For Windows, you should replace python3 with the full path to python.exe:
>C:\Python34\python.exe -m venv ENV_DIR
(This is a typical Python installation; your system may vary.)
In older versions of Python, including Python 2, one of the following commands should work in most cases:
$ virtualenv ENV_DIR
$ venv ENV_DIR
$ pyvenv ENV_DIR
$ pyvenv3 ENV_DIR
ENV_DIR should be a non-existent directory. The directory can have any name, but to keep these instructions simple, I will assume you have created your virtualenv in a directory called venv (e.g. with python3 -m venv ./venv).
To work in your virtualenv, you activate it:
$ . ./venv/bin/activate
(venv)$
Or use this if you have a windows system:
$ venv\Scripts\activate
The (venv) in the shell prompt lets you know which virtualenv you have activated, but you can turn this feature off if you do not like it. You can run all the usual Python commands, and they will be local to your virtualenv:
(venv)$ pip install requests numpy
[...]
(venv)$ python
[...]
>>> import requests
>>> import numpy as np
>>>
python will run the version of Python that you installed into your virtualenv, so (for example) you don't have to type python3 to get Python 3. The Python that it runs will have access to all the standard library modules and all the packages you installed into the virtualenv, but (by default) none of the packages installed in the system-wide site-packages directory.
This last rule is important: by restricting your virtualenv to only use locally-installed packages, you can ensure that you control exactly which dependencies your project is using, even if some new system-wide package gets installed or updated next week. If you like, you can get a listing of your installed packages:
(venv)$ pip freeze
requests==2.13.0
numpy==1.12.0
(venv)$
pip can also parse this format and install from it, and it will install the same versions, even if updates have been released in the meantime:
(venv)$ pip freeze >requirements.txt
(some-other-venv)$ pip install -r requirements.txt
[...]
(some-other-venv)$ python
>>> import requests
>>> import numpy as np
>>>
You can get out of the virtualenv by deactivating it:
(venv)$ deactivate
$ python
[...]
>>> import requests
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'requests'
You can create as many virtualenvs as you like, and they won't interfere with each other, nor with your system packages. A virtualenv is "just" a directory with a bunch of binaries and scripts under it, so you can remove a virtualenv the same way you remove any directory (rm -r venv on Unix). If the virtualenv is activated when you remove it, you may confuse your shell, so it's probably a good idea to deactivate first in that case.
Some times you are not given root privileges and you might end up not being able to use sudo. Many other times, it's not advisable to use sudo to install packages as it might overwrite some package which might be in use by some other applications.
Virtualenv can help you create a separate environment where you don't need root privileges as well as be able to tailor the environment according to your need. It consists of self-contained python installation which only interacts with your specific created environment.
So basically, it gives you a bit of freedom as well as avoid damaging (or modifying) the root environment which might be hosting many old functionalities of old applications.
Installation is pretty easy too.
Installing packages with sudo pip will install packages globally, which may break some system tools.
By install globally it means you will install your packages in place like /usr/lib/python2.7/site-package so if some packages need a previous version of your python packages, this action may break it.
virtualenv allows you to avoid installing Python packages globally by making an isolated python environment. That means it will install packages just in your desire project folder.
On mac and linux
Install
python3 -m pip install --user virtualenv
Creating a Virtual Env: Go to your desired project folder
python3 -m virtualenv env
Activating a virtualenv: In your desired project folder
source env/bin/activate
After activating you can install your packages using pip.
For more information about using it in Windows:
How to use virtualenv in Windows
I am going to break your question into two parts.
What is a virtualenv?
Python has its own way of downloading, storing, and resolving site packages. But Python can not differentiate between different versions in the site-package directory. Packages will be installed in one of the directories, whose name can be found by running the site.getsitepackages() commands.
>>> import site
>>> site.getsitepackages()
This means package_v2.0.1 and package_v3.0.1 have to be in the same directory with the same name i.e. package, which is obviously not possible. Now, you may ask why we would need the same package with different versions on our system. This is because multiple projects may require different versions of Python packages or even different Python versions themselves. So there was a need to have something which will resolve these conflicts and Virtualenv came into the picture to solve this issue.
What does it do for me?
It isolates the environment for Python projects so that each project can have its own dependencies.