3

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
user1592380
  • 34,265
  • 92
  • 284
  • 515
  • i think you need reload functools where that `import functools` is ! and change it to `import reload(functools)` – Mazdak Sep 19 '14 at 18:36
  • Sorry, I'm pretty new to python. would you mind explaining further? What and where is functools ? – user1592380 Sep 19 '14 at 18:56
  • as in your `Traceback ` error says you have this import error `ImportError: No module named functools` so in python if you change a module or delete and built it again you need to reload module so i suggest reload `functools` i think its in this path `ImportError: No module named functools` – Mazdak Sep 19 '14 at 19:00
  • add `reload(functools)` above of `import functools` than give me the result ! – Mazdak Sep 19 '14 at 19:15
  • OK , please see above. – user1592380 Sep 19 '14 at 20:07
  • where you add that you must add it in a `.py` file ! – Mazdak Sep 19 '14 at 20:10
  • I'm not sure I understand what you are saying correctly. I've created a new file called test.py and tried both: "reload(functools.py)" and "reload(functools)" in it. the I ran it. Please see above: – user1592380 Sep 20 '14 at 13:27
  • As we can see from the stacktrace, the error occurs before test.py is executed. could you check if the lib directory has the file `functools.py` it is one of the python modules that is imported by the interpreter before executing any file? – srj Sep 23 '14 at 19:56
  • there is no functools.py in C:\envs\r1\Lib – user1592380 Sep 23 '14 at 20:55
  • 1
    this advice to reload functools, in python code, makes no sense to me – Anentropic Sep 26 '14 at 17:15

2 Answers2

4

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.

Anton Strogonoff
  • 32,294
  • 8
  • 53
  • 61
2

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
Community
  • 1
  • 1
Emily
  • 316
  • 2
  • 9
  • Emily , thanks for looking at this. What directory should I execute the pip freeze from? – user1592380 Sep 24 '14 at 14:13
  • Thanks Emily, when I do the pip freeze I get "ImportError: No module named functools" just like above. – user1592380 Sep 24 '14 at 19:31
  • At this point I'm beginning to suspect that I corrupted something when I deleted the python275 folder. If someone could show me how to rebuild from scratch using newly installed python275 folder as the interpreter I would accept this as an answer. - Bill – user1592380 Sep 26 '14 at 00:23
  • Sorry I couldn't answer before, check this post, seems like the same problem you're having: http://stackoverflow.com/questions/16786287/changing-virtualenv-folder-on-windows – Emily Sep 26 '14 at 08:57
  • Thanks for your hard work on this. it didn't hrlp me but I'm awarding the bounty to you – user1592380 Sep 29 '14 at 19:05