<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ca">
	<id>http://wiki.joanillo.org/index.php?action=history&amp;feed=atom&amp;title=Python_Virtual_Environments</id>
	<title>Python Virtual Environments - Historial de revisió</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.joanillo.org/index.php?action=history&amp;feed=atom&amp;title=Python_Virtual_Environments"/>
	<link rel="alternate" type="text/html" href="http://wiki.joanillo.org/index.php?title=Python_Virtual_Environments&amp;action=history"/>
	<updated>2026-09-21T20:33:26Z</updated>
	<subtitle>Historial de revisió per a aquesta pàgina del wiki</subtitle>
	<generator>MediaWiki 1.34.2</generator>
	<entry>
		<id>http://wiki.joanillo.org/index.php?title=Python_Virtual_Environments&amp;diff=258865&amp;oldid=prev</id>
		<title>Joan: /* utilitzar o no site-packages */</title>
		<link rel="alternate" type="text/html" href="http://wiki.joanillo.org/index.php?title=Python_Virtual_Environments&amp;diff=258865&amp;oldid=prev"/>
		<updated>2020-02-03T13:24:45Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;utilitzar o no site-packages&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Pàgina nova&lt;/b&gt;&lt;/p&gt;&lt;div&gt;__TOC__&lt;br /&gt;
=Introducció=&lt;br /&gt;
En el procés d'aprenentatge de TensorFlow m'he fet molt embolic amb les versions de les llibreries. És possible seguir un tutorial, i les llibreries que necessito són més antigues que les que tinc. O al revés. A més, puc treballar amb varis projectes alhora, que necessien la mateixa llibreria però diferents versions. És en aquest context que va bé treballar amb els Entorns Virtuals.&lt;br /&gt;
&lt;br /&gt;
=Teoria=&lt;br /&gt;
Seguim:&lt;br /&gt;
*https://realpython.com/python-virtual-environments-a-primer/&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import sys&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; sys.prefix&lt;br /&gt;
'/usr'&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import site&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; site.getsitepackages()&lt;br /&gt;
['/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.6/dist-packages']&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
At its core, the main purpose of Python virtual environments is to create an isolated environment for Python projects. This means that each project can have its own dependencies, regardless of what dependencies every other project has.&lt;br /&gt;
&lt;br /&gt;
In our little example above, we’d just need to create a separate virtual environment for both ProjectA and ProjectB, and we’d be good to go. Each environment, in turn, would be able to depend on whatever version of ProjectC they choose, independent of the other.&lt;br /&gt;
&lt;br /&gt;
The great thing about this is that there are no limits to the number of environments you can have since they’re just directories containing a few scripts. Plus, they’re easily created using the virtualenv or pyenv command line tools.&lt;br /&gt;
&lt;br /&gt;
If you are using Python 3, then you should already have the '''venv''' module from the standard library installed. (no cal instal·lar el mòdul '''virtualenv''').&lt;br /&gt;
&lt;br /&gt;
Start by making a new directory to work with:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ mkdir python-virtual-environments &amp;amp;&amp;amp; cd python-virtual-environments&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Create a new virtual environment inside the directory:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Python 2:&lt;br /&gt;
$ virtualenv env&lt;br /&gt;
&lt;br /&gt;
# Python 3&lt;br /&gt;
$ python3 -m venv env -&amp;gt; aquesta és la meva opció&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Note: By default, this will not include any of your existing site packages.&lt;br /&gt;
&lt;br /&gt;
Però en Ubuntu necessitor fer:&lt;br /&gt;
&lt;br /&gt;
On Debian/Ubuntu systems, you need to install the python3-venv&lt;br /&gt;
package using the following command.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ sudo apt-get install python3-venv&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ python3 -m venv env&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Per tant, dins d'aquest entorn no disposo dels paquets que podia tenir en l'entorn normal, com ara ''numpy''.&lt;br /&gt;
&lt;br /&gt;
dins el directori s'ha creat una carpeta ''env/'' que conté una estructura similar a:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
├── bin&lt;br /&gt;
│   ├── activate&lt;br /&gt;
│   ├── activate.csh&lt;br /&gt;
│   ├── activate.fish&lt;br /&gt;
│   ├── easy_install&lt;br /&gt;
│   ├── easy_install-3.5&lt;br /&gt;
│   ├── pip&lt;br /&gt;
│   ├── pip3&lt;br /&gt;
│   ├── pip3.5&lt;br /&gt;
│   ├── python -&amp;gt; python3.5&lt;br /&gt;
│   ├── python3 -&amp;gt; python3.5&lt;br /&gt;
│   └── python3.5 -&amp;gt; /Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5&lt;br /&gt;
├── include&lt;br /&gt;
├── lib&lt;br /&gt;
│   └── python3.5&lt;br /&gt;
│       └── site-packages&lt;br /&gt;
└── pyvenv.cfg&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
*bin: files that interact with the virtual environment&lt;br /&gt;
*include: C headers that compile the Python packages -&amp;gt; a mida que instal·li paquets veuré com es van creant carpetes que fan referència als paquets.&lt;br /&gt;
*lib: a copy of the Python version along with a site-packages folder where each dependency is installed&lt;br /&gt;
&lt;br /&gt;
In order to use this environment’s packages/resources in isolation, you need to ''activate'' it. To do this, just run the following:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ source env/bin/activate&lt;br /&gt;
(env) joan@joanHP:~/python-virtual-environments$&lt;br /&gt;
(env) joan@joanHP:~/python-virtual-environments$ PS1=&amp;quot;E $ &amp;quot; -&amp;gt; canvio el prompt&lt;br /&gt;
E $&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
This is the indicator that env is currently active, which means the python executable will only use this environment’s packages and settings.&lt;br /&gt;
&lt;br /&gt;
Per invocar a ''python3'' no cal fer ''python3'' sinó tan sols ''python'', doncs he instal·lat l'entorn virtual de python3. Per exemple, en aquest entorn virtual, numpy no està disponible (en l'entorn host sí que ho està).&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
E $ python&lt;br /&gt;
Python 3.6.9 (default, Nov  7 2019, 10:44:02) &lt;br /&gt;
[GCC 8.3.0] on linux&lt;br /&gt;
Type &amp;quot;help&amp;quot;, &amp;quot;copyright&amp;quot;, &amp;quot;credits&amp;quot; or &amp;quot;license&amp;quot; for more information.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import numpy&lt;br /&gt;
Traceback (most recent call last):&lt;br /&gt;
  File &amp;quot;&amp;lt;stdin&amp;gt;&amp;quot;, line 1, in &amp;lt;module&amp;gt;&lt;br /&gt;
ModuleNotFoundError: No module named 'numpy'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Si volem tornar al ''system context'', hem de desactivar l'entorn virtual:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
(env) $ deactivate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
En el host:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ which python&lt;br /&gt;
/usr/bin/python&lt;br /&gt;
&lt;br /&gt;
$ which python3&lt;br /&gt;
/usr/bin/python3&lt;br /&gt;
&lt;br /&gt;
$ echo $PATH&lt;br /&gt;
/home/joan/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I en l'entorn virtual:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ source env/bin/activate&lt;br /&gt;
&lt;br /&gt;
E $ which python&lt;br /&gt;
/home/joan/python-virtual-environments/env/bin/python&lt;br /&gt;
&lt;br /&gt;
E $ which python3 (és el mateix)&lt;br /&gt;
/home/joan/python-virtual-environments/env/bin/python3&lt;br /&gt;
&lt;br /&gt;
E $ echo $PATH&lt;br /&gt;
/home/joan/python-virtual-environments/env/bin:/home/joan/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Our virtual environment's bin directory is now at the beginning of the path. That means it's the first directory searched when running an executable on the command line. Thus, the shell uses our virtual environment's instance of Python instead of the system-wide version.&lt;br /&gt;
&lt;br /&gt;
This can be explained by how Python starts up and where it is located on the system. There actually isn't any difference between these two Python executables. It's their directory locations that matter.&lt;br /&gt;
&lt;br /&gt;
When Python is starting up, it looks at the path of its binary. In a virtual environment, it is actually just a copy of, or symlink to, your system’s Python binary. It then sets the location of sys.prefix and sys.exec_prefix based on this location, omitting the bin portion of the path.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
E $ python&lt;br /&gt;
Python 3.6.9 (default, Nov  7 2019, 10:44:02) &lt;br /&gt;
[GCC 8.3.0] on linux&lt;br /&gt;
Type &amp;quot;help&amp;quot;, &amp;quot;copyright&amp;quot;, &amp;quot;credits&amp;quot; or &amp;quot;license&amp;quot; for more information.&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import sys&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; sys.prefix&lt;br /&gt;
'/home/joan/python-virtual-environments/env'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
==Eliminar un entorn virtual==&lt;br /&gt;
No em queda clar com s'elimina un entorn virtual creat amb la manera de ''python3''. Per tant, ho faré d'aquesat manera (que és també com es fa en el tutorial de ''tensorflow''):&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ virtualenv --system-site-packages -p python3 env1&lt;br /&gt;
Already using interpreter /usr/bin/python3&lt;br /&gt;
Using base prefix '/usr'&lt;br /&gt;
New python executable in /home/joan/projectes/tensorflow_tutorial/env1/bin/python3&lt;br /&gt;
Also creating executable in /home/joan/projectes/tensorflow_tutorial/env1/bin/python&lt;br /&gt;
Installing setuptools, pip, wheel...&lt;br /&gt;
done.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
I ara eliminem l'entorn virtual:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ virtualenv --clear env1&lt;br /&gt;
Deleting tree /home/joan/projectes/tensorflow_tutorial/env1/lib/python3.6&lt;br /&gt;
Not deleting /home/joan/projectes/tensorflow_tutorial/env1/bin&lt;br /&gt;
Using base prefix '/usr'&lt;br /&gt;
New python executable in /home/joan/projectes/tensorflow_tutorial/env1/bin/python3&lt;br /&gt;
Not overwriting existing python script /home/joan/projectes/tensorflow_tutorial/env1/bin/python (you must use /home/joan/projectes/tensorflow_tutorial/env1/bin/python3)&lt;br /&gt;
Installing setuptools, pip, wheel...&lt;br /&gt;
done.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Encara queda la carpeta ''env1''. Suposo que puc eliminar-la tal qual&lt;br /&gt;
&lt;br /&gt;
En el tutorial s'explica tot el tema de ''virtualenvwrapper''. Però jo no he fet (l'he hagut de desintal·lar, perquè té ganes de treballar amb python2 i no python3).&lt;br /&gt;
==utilitzar o no site-packages==&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ virtualenv --no-site-packages -p python3 env1&lt;br /&gt;
Already using interpreter /usr/bin/python3&lt;br /&gt;
...&lt;br /&gt;
done.&lt;br /&gt;
&lt;br /&gt;
$ source env1/bin/activate&lt;br /&gt;
(env1) ...$ python3 -m pip freeze | grep tensor&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
No troba res, que és lo correcte. Si hagués utilitzat l'opció ''system-site-packages'', i hagués instal·lat el tensorflow, m'apareixeria en el llistat. I això pot ser una font de conflictes doncs ja no sé què tinc instal·lat en el sistema (host) i en l'entorn virtual.&lt;br /&gt;
&lt;br /&gt;
Per tant, per fer proves amb ''tensorflow'' (que és per això que estic aquí), desinstal·lo el tensorflow del sistema.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ virtualenv --system-site-packages -p python3 env1&lt;br /&gt;
Already using interpreter /usr/bin/python3&lt;br /&gt;
...&lt;br /&gt;
done.&lt;br /&gt;
&lt;br /&gt;
$ source env1/bin/activate&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Joan</name></author>
		
	</entry>
</feed>