Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make mkvirtualenv WORKON_HOME environment variable aware #21

Closed
felciano opened this issue Aug 31, 2012 · 10 comments
Closed

Make mkvirtualenv WORKON_HOME environment variable aware #21

felciano opened this issue Aug 31, 2012 · 10 comments

Comments

@felciano
Copy link

When creating a new virtual environment it creates an activate.bat file with a hardcoded pathname at the top:

@echo off
set VIRTUAL_ENV=C:\Dropbox\virtualenvs\myproject

This is because WORKON_HOME is set to C:\Dropbox\virtualenvs. However on other machines Dropbox may be located elsewhere, for example F:\Dropbox\virtualenvs, which breaks the activation script.

Is it possible to change the virtualenv creation to use the environment variable instead? That is, change the above to:

@echo off
set VIRTUAL_ENV=%WORKON_HOME%\myproject
@piotr-dobrogost
Copy link
Collaborator

How do you know it's only activate.bat that needs to be fixed in order for virtualenv to work correctly in different location?
The assumption is virtualenv once created can be made relocatable by using --relocatable option of virtualenv. However this option does not work under Windows;

Z:\>virtualenv testenv
New python executable in testenv\Scripts\python.exe
Installing distribute............................................................................................................................................................................................................done.
Installing pip..................done.

Z:\>virtualenv --relocatable testenv
Script testenv\Scripts\activate.ps1 cannot be made relative (it's not a normal script that starts with
#!z:\testenv/bin/python)
Script testenv\Scripts\deactivate.bat cannot be made relative (it's not a normal script that starts with #!z:\testenv/bin/python)
Script testenv\Scripts\easy_install-2.7-script.py cannot be made relative (it's not a normal script that starts with #!z:\testenv/bin/python)
Script testenv\Scripts\easy_install-script.py cannot be made relative (it's not a normal script that starts with #!z:\testenv/bin/python)
Script testenv\Scripts\pip-2.7-script.py cannot be made relative (it's not a normal script that starts
with #!z:\testenv/bin/python)
Script testenv\Scripts\pip-script.py cannot be made relative (it's not a normal script that starts with #!z:\testenv/bin/python)

Z:\>

Apparently it's because it works by modifying shebang line which is not normally supported on Windows (unless you install pylauncher).

I guess we should first ask what's the reason virtualenv itself doesn't support making virtualenvs relocatable on Windows.

@davidmarble
Copy link
Owner

It would be much easier if the virtualenv project already supported this -- it's what hardcodes the path.

You'll see in my code for mkvirtualenv.bat that I do some simple modification of activate.bat to support PYTHONPATH, PYTHONHOME, and the environment variable VIRTUAL_ENV to keep track of whether a virtualenv is activated.

Modifying the first few lines via search & replace sounds like a fun DOS batch script homework assignment. Let me know if you find a solution! Feel free to submit a pull request if you figure out a good way to do this!

@piotr-dobrogost
Copy link
Collaborator

Was the above answer directed to me or the OP?

@davidmarble
Copy link
Owner

OP.

I haven't kept up on virtualenv on Windows. I wrote this project a long time ago and use Linux for pretty much all my programming now.

@felciano
Copy link
Author

Seems like this is was issue #49 in virtualenv (pypa/virtualenv#49), but is marked as closed. Would that address this issue?

@piotr-dobrogost
Copy link
Collaborator

@davidmarble

(...) and the environment variable VIRTUAL_ENV to keep track of whether a virtualenv is activated.

This will not work due to a bug in virtualenv - see pypa/virtualenv#9.

@felciano

Would that address this issue?

Nice find! Not by itself but it appears it's very much needed, yes. The problem with relocation is it's regarded as a misfeature by some of maintainers of virtualenv so I guess it's not high priority.

@davidmarble
Copy link
Owner

@piotr-dobrogost VIRTUAL_ENV usage works fine, because I modify both activate.bat and deactivate.bat when creating a virtualenv. This doesn't address the OP's question, but as you pointed out, it's not a priority of the virtualenv maintainers. That's why I've worked around some virtualenv limitations by modifying activate.batanddeactivate.bat`.

See https://github.com/davidmarble/virtualenvwrapper-win/blob/master/scripts/mkvirtualenv.bat#L77

Edit: I've removed the line that adds unsetting VIRTUAL_ENV in deactivate.bat because the 1.9.x branch of virtualenv now does this (Pitor added it).

@davidmarble
Copy link
Owner

Update: --relocatable is still not yet working in virtualenv. See the latest comments in pypa/virtualenv#49

@plicit
Copy link

plicit commented May 16, 2013

--relocatable seems to be working in 1.10.dev1. Of course, it would still have to be called after subsequent package installs to fixup any new scripts.

On a related note, what do you guys think of a directory-relative launcher?

i.e. the system .py launcher would walk up the executed script's parent directories looking for a .run.python.bat file that would actually launch the script in the right environment.

I think this would:

  • allow simple relocation by just copying directories. e.g. dropbox.
  • allow other programs to easily use the venv. e.g. explorer.
  • allow easy use of multiple pythons in the same shell command without having to explicitly fixup the environment or shebangs. e.g. myscript.py | utility.py where each script resolves to a different directory environment.

@thebjorn
Copy link
Collaborator

thebjorn commented Aug 4, 2017

Relocatable isn't working with the latest virtualenv

c:\srv\tmp\wkonhome> virtualenv --version
15.1.0

c:\srv\tmp\wkonhome> virtualenv --relocatable test
The environment doesn't have a file c:\srv\tmp\wkonhome\test\Scripts\activate_this.py -- please re-run virtualenv on this environment to update it Traceback (most recent call last):
  File "c:\python27\lib\runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "c:\python27\lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "C:\Python27\Scripts\virtualenv.exe\__main__.py", line 9, in <module>
  File "c:\python27\lib\site-packages\virtualenv.py", line 700, in main
    make_environment_relocatable(home_dir)
  File "c:\python27\lib\site-packages\virtualenv.py", line 1588, in make_environment_relocatable
    fixup_scripts(home_dir, bin_dir)
  File "c:\python27\lib\site-packages\virtualenv.py", line 1610, in fixup_scripts
    for filename in os.listdir(bin_dir):
WindowsError: [Error 3] The system cannot find the path specified: 'c:\\srv\\tmp\\wkonhome\\test\\Scripts/*.*'

Based on my reading of the virtualenv issues mentioning relocatable (https://github.com/pypa/virtualenv/issues?utf8=%E2%9C%93&q=is%3Aissue%20is%3Aopen%20relocatable) I wouldn't expect any support coming from that camp.

I think the best thing we can do is implement #65 cpvirtualenv.

I'm closing this as a wontfix..

@thebjorn thebjorn closed this as completed Aug 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants