user1592380
user1592380

Reputation: 36205

How to rebuild virtualenvs after deleting associated python interpreter

I have a number of django projects organized with the following directory structure using win7 (I'm using GIT_BASH/mingw for my command line) :

envs--r1--project1
        --project2
        --Include
        --Library
        --Scripts--python.exe
python275--

The idea being here that the different projects have a common environment and I can activate that environment from each projects root directory using:

$ source ../Scripts/activate

I don't understand exactly how it happened but it turned out that the interpreter ( listed above as python.exe ) was linked somehow to a second python folder above.

I deleted the python275 folder ( AAHH!!) , not realizing its importance, resulting in:

Traceback (most recent call last): File "C:\envs\r1\lib\site.py", line 703, in   main() File "C:\envs\r1\lib\site.py", line 692, in main aliasmbcs() File "C:\envs\r1\lib\site.py", line 515, in aliasmbcs import locale, codecs File "C:\envs\r1\lib\locale.py", line 19, in   import functools ImportError: No module named functools

I have reinstalled the correct python folder but the error persists. Can anyone advise me on how to rebuild the virtualenvs so I can go back to the way it was?

edit:

(r1)
/C/envs/r1/Scripts
$ import reload(functools)
sh: syntax error near unexpected token `('
(r1)
/C/envs/r1/Scripts
$ python.exe import reload(functools)
sh: syntax error near unexpected token `('
(r1)
/C/envs/r1/Scripts
$ python.exe reload functools        
Traceback (most recent call last):
  File "C:\envs\r1\lib\site.py", line 703, in <module>
    main()
  File "C:\envs\r1\lib\site.py", line 692, in main
    aliasmbcs()
  File "C:\envs\r1\lib\site.py", line 515, in aliasmbcs
    import locale, codecs
  File "C:\envs\r1\lib\locale.py", line 19, in <module>
    import functools
ImportError: No module named functools


(r1)
/C/envs/r1/Scripts
$ reload(functools)
sh: syntax error near unexpected token `('
(r1)
/C/envs/r1/Scripts
$ python.exe reload(functools)
sh: syntax error near unexpected token `('
(r1)


/C/envs/r1/Scripts
$ python.exe  test.py
Traceback (most recent call last):
  File "f:\envs\r1\lib\site.py", line 703, in <module>
    main()
  File "f:\envs\r1\lib\site.py", line 692, in main
    aliasmbcs()
  File "f:\envs\r1\lib\site.py", line 515, in aliasmbcs
    import locale, codecs
  File "f:\envs\r1\lib\locale.py", line 19, in <module>
    import functools
ImportError: No module named functools

Upvotes: 3

Views: 3939

Answers (2)

Anton Strogonoff
Anton Strogonoff

Reputation: 34032

I suggest reinstalling Python and virtualenv from scratch in your case, and then re-creating the environment(s).

  • Make sure you have a working Python 2.7.5 installation on your system.
  • Make sure you have pip installed (see docs).
  • Make sure pip is linked to the Python installation above (check the output of pip -V, it should contain the correct system-wide Python path).
  • Do a clean install of virtualenv using pip install virtualenv.
  • Create and activate the environment for your projects.
  • Reinstall within your new environment all the modules you had installed in your old environment.

    This may be a tricky one.

    • If you have an up-to-date file such as requirements.txt with all the packages required, then you're all set (pip install -r requirements.txt should do it).
    • If you don't have requirements listed, or they aren't up-to-date, then check manually which packages you had installed in your old environment. For that you should locate site-packages directory within your old virtualenv (should be under Library probably) and look what's in there. Every non-system package you recognize in site-packages directory you install into your new environment (normally using pip install but some packages may have custom instructions).

That's what I did when it happened to me. I wasn't able to simply rebuild the environment, so I did the reinstall for cleanness.

Upvotes: 4

Emily
Emily

Reputation: 316

Taken from other post. You can associate the virtualenv to a python version.

You indicate the Python interpreter to use with the flag -p or --python (e.g --python=python2.5)

    virtualenv -p /usr/bin/python2.6 <path/to/new/virtualenv/>

But this is just works for creating new environments. You would need to access your old virtualenv and execute:

     pip freeze > requirements.txt

The file requirements.txt would contain all the apps you installed in the old virtual environment and their respective versions. Now, from the new environment you have created, run:

     pip install -r requirements.txt

You should active the virtual environment.

     $ source /yourvirtualenvpath/bin/activate

     $ pip freeze > /home/user/requirements.txt

     $ deactivate 

     $ source /yourNEWvirtualenvpath/bin/activate

     $ pip install -r /home/user/requirements.txt

Upvotes: 2

Related Questions