3

Background: I installed Python 3.5.2 on my Mac (which already contained 2.7.10) and have been apparently running the two installations side-by-side without any apparent issues. Everything works fine until I move the project folder somewhere else, and then when I try to do anything I get the following error:

Traceback (most recent call last): File "manage.py", line 8, in <module> from django.core.management import execute_from_command_line ImportError: No module named django.core.management

My normal setup workflow is as follows:

  • Install a virtual environment in the directory containing the Django project folder (not the directory containing manage.py - one level up from that) with python3 -m venv <venv-name>
  • Activate the virtual environment and install Django, Pillow, whatever I need for the project.

I know I'm missing something because I thought the way virtual environments worked was that you installed them locally and then as long as all of that accompanied your project folder, everything would be a-okay. But everything stops working when I move the directory, and if I move it back it works again.

Can anyone tell me what kind of issue I'm dealing with here based on this? Is this just normal behavior and I just need to get used to not moving Django project folders?

UPDATE: If I delete the virtual environment folder and re-install it once the folder is in the new location everything seems to work fine. I guess it's some issue with the creation of virtual environments and some kind of link to my Python installation? I have no idea.

3
  • Did you move the venv folder together with your project folder?
    – Juergen
    Nov 10, 2017 at 20:35
  • @Juergen - Yes, e.g. I'll have Projects/my_project/my_project, where the contents of the first my_project are only the virtualenv and my_project (which contains manage.py). So I'm just moving the whole folder from Projects/
    – user6806370
    Nov 10, 2017 at 20:39
  • @Juergen So I thought I was onto something because I copy-pasted another project folder to another location and everything was fine, but then I tried deleting it from the original location and now it's not working. So there must be some link between my virtualenv and my Python installation? What could it be?
    – user6806370
    Nov 10, 2017 at 20:43

2 Answers 2

2

Late response, but I want to apport my workaround for this issue. Thanks to the Thomas response I understood the problem by looking at the activate script. So, was easier in my case restore the original path and follow these steps:

  1. Before to move the project and with the virtual environment activated

    pip freeze > requirements.txt

  2. Then deactivate the environment and remove it

    deactivate
    rm -rf .env

  3. Move the project to the desired directory

  4. Generate a fresh new virtual env and activate it

    python3 -m venv .env
    source .env/bin/activate

  5. Install the dependencies

    pip install -r requirements.txt

1

Blind guess:

1) Did you activate your Virtual Environment? It sounds like manage.py couldn't find the django installation.

2) Has the Environment Django installed?

3) Do you move the env itself? The generated activate-script has hardcoded ( mine: e.g. VIRTUAL_ENV="/home/thomas/Intevation/env" ) paths in it. I would recommend to make a new env and install it there.

I encourage people to use: Pipenv


The traceback is a clear indicator, that wherever manage.py looks, is no django installed.

2
  • 1
    1) Haha, yes -- unfortunately I made that mistake earlier on but fortunately caught myself before coming here for help. 2) Yes, the only two packages I install for this particular project are django and Pillow
    – user6806370
    Nov 10, 2017 at 20:52
  • Okay the hard-coded paths in the activate script makes sense. Thanks very much! I'll take a look at Pipenv!
    – user6806370
    Nov 10, 2017 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.