ºìÁªLinuxÃÅ»§
Linux°ïÖú

Python ÖÐ virtualenv ʹÓ÷½·¨

·¢²¼Ê±¼ä:2017-09-29 12:36:19À´Ô´:ºìÁª×÷Õß:Ronny
ÔÚ¿ª·¢PythonÓ¦ÓóÌÐòµÄʱºò£¬ÏµÍ³°²×°µÄPython3Ö»ÓÐÒ»¸ö°æ±¾£º3.4¡£ËùÓеÚÈý·½µÄ°ü¶¼»á±»pip°²×°µ½Python3µÄsite-packagesĿ¼Ï¡£
Èç¹ûÎÒÃÇҪͬʱ¿ª·¢¶à¸öÓ¦ÓóÌÐò£¬ÄÇÕâЩӦÓóÌÐò¶¼»á¹²ÓÃÒ»¸öPython£¬¾ÍÊÇ°²×°ÔÚϵͳµÄPython 3¡£Èç¹ûÓ¦ÓÃAÐèÒªjinja 2.7£¬¶øÓ¦ÓÃBÐèÒªjinja 2.6Ôõô°ì£¿
ÕâÖÖÇé¿öÏ£¬Ã¿¸öÓ¦ÓÿÉÄÜÐèÒª¸÷×ÔÓµÓÐÒ»Ìס°¶ÀÁ¢¡±µÄPythonÔËÐл·¾³¡£virtualenv¾ÍÊÇÓÃÀ´ÎªÒ»¸öÓ¦Óô´½¨Ò»Ìס°¸ôÀ롱µÄPythonÔËÐл·¾³¡£
Ê×ÏÈ£¬ÎÒÃÇÓÃpip°²×°virtualenv£º
$ pip3 install virtualenv
È»ºó£¬¼Ù¶¨ÎÒÃÇÒª¿ª·¢Ò»¸öеÄÏîÄ¿£¬ÐèÒªÒ»Ì׶ÀÁ¢µÄPythonÔËÐл·¾³£¬¿ÉÒÔÕâô×ö£º
µÚÒ»²½£¬´´½¨Ä¿Â¼£º
Mac:~ michael$ mkdir myproject Mac:~ michael$ cd myproject/ Mac:myproject michael$
µÚ¶þ²½£¬´´½¨Ò»¸ö¶ÀÁ¢µÄPythonÔËÐл·¾³£¬ÃüÃûΪvenv£º
Mac:myproject michael$ virtualenv --no-site-packages venv Using base prefix '/usr/local/.../Python.framework/Versions/3.4' New python executable in venv/bin/python3.4 Also creating executable in venv/bin/python Installing setuptools, pip, wheel...done.
ÃüÁîvirtualenv¾Í¿ÉÒÔ´´½¨Ò»¸ö¶ÀÁ¢µÄPythonÔËÐл·¾³£¬ÎÒÃÇ»¹¼ÓÉÏÁ˲ÎÊý--no-site-packages£¬ÕâÑù£¬ÒѾ­°²×°µ½ÏµÍ³Python»·¾³ÖеÄËùÓеÚÈý·½°ü¶¼²»»á¸´ÖƹýÀ´£¬ÕâÑù£¬ÎÒÃǾ͵õ½ÁËÒ»¸ö²»´øÈκεÚÈý·½°üµÄ¡°¸É¾»¡±µÄPythonÔËÐл·¾³¡£
±¾ÎÄתÔØ×Ô£ºhttp://www.linuxprobe.com/python-virtualenv.html
Ãâ·ÑÌṩ×îÐÂLinux¼¼Êõ½Ì³ÌÊé¼®£¬Îª¿ªÔ´¼¼Êõ°®ºÃÕßŬÁ¦×öµÃ¸ü¶à¸üºÃ£¬¿ªÔ´Õ¾µã£ºhttp://www.linuxprobe.com/
н¨µÄPython»·¾³±»·Åµ½µ±Ç°Ä¿Â¼ÏµÄvenvĿ¼¡£ÓÐÁËvenvÕâ¸öPython»·¾³£¬¿ÉÒÔÓÃsource½øÈë¸Ã»·¾³£º
Mac:myproject michael$ source venv/bin/activate (venv)Mac:myproject michael$
×¢Òâµ½ÃüÁîÌáʾ·û±äÁË£¬Óиö(venv)ǰ׺£¬±íʾµ±Ç°»·¾³ÊÇÒ»¸öÃûΪvenvµÄPython»·¾³¡£
ÏÂÃæÕý³£°²×°¸÷ÖÖµÚÈý·½°ü£¬²¢ÔËÐÐpythonÃüÁ
(venv)Mac:myproject michael$ pip install jinja2 ... Successfully installed jinja2-2.7.3 markupsafe-0.23 (venv)Mac:myproject michael$ python myapp.py ...
ÔÚvenv»·¾³Ï£¬ÓÃpip°²×°µÄ°ü¶¼±»°²×°µ½venvÕâ¸ö»·¾³Ï£¬ÏµÍ³Python»·¾³²»ÊÜÈκÎÓ°Ïì¡£Ò²¾ÍÊÇ˵£¬venv»·¾³ÊÇרÃÅÕë¶ÔmyprojectÕâ¸öÓ¦Óô´½¨µÄ¡£
Í˳öµ±Ç°µÄvenv»·¾³£¬Ê¹ÓÃdeactivateÃüÁ
(venv)Mac:myproject michael$ deactivate Mac:myproject michael$
´Ëʱ¾Í»Øµ½ÁËÕý³£µÄ»·¾³£¬ÏÖÔÚpip»òpython¾ùÊÇÔÚϵͳPython»·¾³ÏÂÖ´ÐС£
ÍêÈ«¿ÉÒÔÕë¶Ôÿ¸öÓ¦Óô´½¨¶ÀÁ¢µÄPythonÔËÐл·¾³£¬ÕâÑù¾Í¿ÉÒÔ¶Ôÿ¸öÓ¦ÓõÄPython»·¾³½øÐиôÀë¡£
virtualenvÊÇÈçºÎ´´½¨¡°¶ÀÁ¢¡±µÄPythonÔËÐл·¾³µÄÄØ£¿Ô­ÀíºÜ¼òµ¥£¬¾ÍÊÇ°ÑϵͳPython¸´ÖÆÒ»·Ýµ½virtualenvµÄ»·¾³£¬ÓÃÃüÁîsource venv/bin/activate½øÈëÒ»¸övirtualenv»·¾³Ê±£¬virtualenv»áÐÞ¸ÄÏà¹Ø»·¾³±äÁ¿£¬ÈÃÃüÁîpythonºÍpip¾ùÖ¸Ïòµ±Ç°µÄvirtualenv»·¾³¡£
С½á
virtualenvΪӦÓÃÌṩÁ˸ôÀëµÄPythonÔËÐл·¾³£¬½â¾öÁ˲»Í¬Ó¦Óüä¶à°æ±¾µÄ³åÍ»ÎÊÌâ¡£
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 1 ÌõÆÀÂÛ

  1. Ronny ÓÚ 2017-09-29 12:36:24·¢±í:

    Python ÖÐ virtualenv ʹÓ÷½·¨