1

Previously I had Virtual Environment installed on my system. I created two different VirtualEnv for two different projects I was working on.
I use to activate one of these virtual environment and would start working on project.
Recently my system got corrupted and I upgraded the OS after copying all projects and virtual environments. I copied the project files and virtual environments back to my system and installed virtual environment.

Set PYTHONHOME='/usr/lib/python3' in .bashrc

Now when I activated one of the copied environment and tried to run python manage.py runserver, I got below error

Could not find platform independent libraries <prefix>
Could not find platform dependent libraries <exec_prefix>
Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>]
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
Aborted (core dumped)

Running pip inside this virtual env is throwing same error.

Please suggest me what I need to so that my project can work as before.

Anurag Rana
  • 1,429
  • 2
  • 24
  • 48

3 Answers3

2

It's not recommended to move a virual environment i.e. to a new system, or a different OS installation - A SO answer.

Good practice is to generate the requirements list (installed Python packages) - while virtual environment is active, execute:

$(venv)  pip freeze > requirements_venv.txt

On the new installation (delete the copied venv if you have it), setup a fresh virtual environment using Python 3, activate it, and then install the python packages:

$ virtualenv -p /usr/bin/python3 venv/
$ source env/bin/activate
$ pip install -r requirements_venv.txt

Probable issue:

Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
Aborted (core dumped)

States, the system's Python 3 is trying to load something that was written for some other Python version i.e. Python 2.x.

Most probable reason for the errors you're getting can be,

  • Created virtual environment with a different python version.
  • Improper PYTHONPATH,
  • Not restarting the terminal after setting PYTHONPATH

Solution/way-out:

First check if you're able to get into the Python shell i.e. execute python or python3 command in the terminal. If not, unset the PYTHONPATH in .bashrc, and execute the following commands in the terminal (or whatever the exact path is on your system):

# export PYTHONHOME=/usr/local/lib/python3.5/
# export PYTHONPATH=/usr/local/lib/python3.5

Now again try to start the Python shell i.e. i.e. execute python or python3 command in the terminal, you must get something similar like below:

Python 3.5.2 (default, Jul 17 2016, 00:00:00) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

With Python shell working, most probably you may not face the posted issue - if you do, delete the copied virtual environment and install and new one (as stated at the top).

Community
  • 1
  • 1
Nabeel Ahmed
  • 18,328
  • 4
  • 58
  • 63
0

Is python correctly installed?

run python in cmd and check the path also

If some dependencies are broken then Try reinstall it correctly

sudo apt-get install --reinstall python2.7

sudo apt-get update

Now try to use virtualenv by

pip install virtualenv <name>
 or 
pip install virtualenv .

active it by source ./bin/activate

Shivkumar kondi
  • 6,458
  • 9
  • 31
  • 58
0

Virtual environments uses some symlinks back to the source python installation in order to share resources and binaries. I think it is very doubtful that you should be able to move a virtual environment to a new/different system and have it working out of the box.