Python virtualenv questions

前端 未结 5 1418
轻奢々
轻奢々 2020-11-28 03:17

I\'m using VirtualEnv on Windows XP. I\'m wondering if I have my brain wrapped around it correctly:

I ran virtualenv ENV and it created C:\\WINDOW

相关标签:
5条回答
  • 2020-11-28 03:45

    Normally virtualenv creates environments in the current directory. Unless you're intending to create virtual environments in C:\Windows\system32 for some reason, I would use a different directory for environments.

    You shouldn't need to mess with paths: use the activate script (in <env>\Scripts) to ensure that the Python executable and path are environment-specific. Once you've done this, the command prompt changes to indicate the environment. You can then just invoke easy_install and whatever you install this way will be installed into this environment. Use deactivate to set everything back to how it was before activation.

    Example:

    c:\Temp>virtualenv myenv
    New python executable in myenv\Scripts\python.exe
    Installing setuptools..................done.
    c:\Temp>myenv\Scripts\activate
    (myenv) C:\Temp>deactivate
    C:\Temp>
    

    Notice how I didn't need to specify a path for deactivate - activate does that for you, so that when activated "Python" will run the Python in the virtualenv, not your system Python. (Try it - do an import sys; sys.prefix and it should print the root of your environment.)

    You can just activate a new environment to switch between environments/projects, but you'll need to specify the whole path for activate so it knows which environment to activate. You shouldn't ever need to mess with PATH or PYTHONPATH explicitly.

    If you use Windows Powershell then you can take advantage of a wrapper. On Linux, the virtualenvwrapper (the link points to a port of this to Powershell) makes life with virtualenv even easier.

    Update: Not incorrect, exactly, but perhaps not quite in the spirit of virtualenv. You could take a different tack: for example, if you install Django and anything else you need for your site in your virtualenv, then you could work in your project directory (where you're developing your site) with the virtualenv activated. Because it was activated, your Python would find Django and anything else you'd easy_installed into the virtual environment: and because you're working in your project directory, your project files would be visible to Python, too.

    Further update: You should be able to use pip, distribute instead of setuptools, and just plain python setup.py install with virtualenv. Just ensure you've activated an environment before installing something into it.

    0 讨论(0)
  • 2020-11-28 03:51

    in my project wsgi.py file i have this code (it works with virtualenv,django,apache2 in windows and python 3.4)

    import os
    import sys
    DJANGO_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)),'..')
    sys.path.append(DJANGO_PATH)
    sys.path.append('c:/myproject/env/Scripts')
    sys.path.append('c:/myproject/env/Lib/site-packages')
    activate_this = 'c:/myproject/env/scripts/activate_this.py'
    exec(open(activate_this).read())
    from django.core.wsgi import get_wsgi_application
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
    application = get_wsgi_application()
    

    in virtualhost file conf i have

    <VirtualHost *:80>
        ServerName mysite
        WSGIScriptAlias / c:/myproject/myproject/myproject/wsgi.py
        DocumentRoot c:/myproject/myproject/
        <Directory  "c:/myproject/myproject/myproject/">
           Options +Indexes +FollowSymLinks +MultiViews
           AllowOverride All
          Require local
       </Directory>
    </VirtualHost>
    
    0 讨论(0)
  • 2020-11-28 03:53

    After creating virtual environment copy the activate.bat file from Script folder of python and paste to it your environment and open cmd from your virtual environment and run activate.bat file.enter image description here

    0 讨论(0)
  • 2020-11-28 03:55

    Yes basically this is what virtualenv do , and this is what the activate command is for, from the doc here:

    activate script

    In a newly created virtualenv there will be a bin/activate shell script, or a Scripts/activate.bat batch file on Windows.

    This will change your $PATH to point to the virtualenv bin/ directory. Unlike workingenv, this is all it does; it's a convenience. But if you use the complete path like /path/to/env/bin/python script.py you do not need to activate the environment first. You have to use source because it changes the environment in-place. After activating an environment you can use the function deactivate to undo the changes.

    The activate script will also modify your shell prompt to indicate which environment is currently active.

    so you should just use activate command which will do all that for you:

    > \path\to\env\bin\activate.bat
    
    0 讨论(0)
  • 2020-11-28 03:57

    on Windows I have python 3.7 installed and I still couldn't activate virtualenv from Gitbash with ./Scripts/activate although it worked from Powershell after running Set-ExecutionPolicy Unrestricted in Powershell and changing the setting to "Yes To All".

    I don't like Powershell and I like to use Gitbash, so to activate virtualenv in Gitbash first navigate to your project folder, use ls to list the contents of the folder and be sure you see "Scripts". Change directory to "Scripts" using cd Scripts, once you're in the "Scripts" path use . activate to activate virtualenv. Don't forget the space after the dot.

    0 讨论(0)
提交回复
热议问题