Friday, February 18, 2022

[SOLVED] Creating a new virtualenv project and creating a virtualenv for existing project causes error

Issue

I've tried to search for a solution or a thread handling this problem but I have a hard time expressing it in the first place since I'm fairly new to django, virtualenv, pip etc.

I'm using windows 10 and I do everything below in powershell.

When I started my first project, named fadderjobb, I didn't use virtualenv. I installed a couple of packages using pip install. Now when I started my second project, wingqvist, I installed virtualenv, made a virtual environment in a folder envs/wingqvist, installed Django and created a new Django project in another new folder webdev/wingqvist. Looking something like this.

webdev
|-- fadderjobb
|   `-- projectfiles for fadderjobb
|-- wingqvist
|   `-- projectfiles for wingqvist
`-- envs
    `-- wingqvist
        `-- virualenv files for wingqvist

When I in my virtualenv (wingqvist) tried to run python manage.py migrate in webdev/wingqvist/ i got the following error:

(wingqvist) PS C:\Users\Viktor Wingqvist\documents\webdev\wingqvist> python 

manage.py migrate
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Viktor Wingqvist\documents\webdev\envs\wingqvist\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "C:\Users\Viktor Wingqvist\documents\webdev\envs\wingqvist\lib\site-packages\django\core\management\__init__.py", line 347, in execute
    django.setup()
  File "C:\Users\Viktor Wingqvist\documents\webdev\envs\wingqvist\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\Viktor Wingqvist\documents\webdev\envs\wingqvist\lib\site-packages\django\apps\registry.py", line 89, in populate
    app_config = AppConfig.create(entry)
  File "C:\Users\Viktor Wingqvist\documents\webdev\envs\wingqvist\lib\site-packages\django\apps\config.py", line 90, in create
    module = import_module(entry)
  File "C:\Users\Viktor Wingqvist\documents\webdev\envs\wingqvist\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'mathfilters'

I recognize 'mathfilters' as a packet I use in fadderjobb so I thought that it somehow (!?) ended up automatically as a dependency in my (wingqvist) virtualenv.

I tried to solve it by creating another virtualenv for fadderjobb, save all installed pip packages in a requirements.txt, run pip uninstall -r requirements.txt, activate virualenv (fadderjobb) and pip install everything in requirements.txt.

I now have a folder/file structure looking something like:

webdev
|-- fadderjobb
|   `-- projectfiles for fadderjobb
|-- wingqvist
|   `-- projectfiles for wingqvist
`-- envs
    |-- wingqvist
    |   `-- virualenv files for wingqvist
    `-- fadderjobb
        `-- virualenv files for fadderjobb

To be sure I tested my old project in (fadderjobb) and it looks great, pip freeze returns what I want etc.

I deactivated (fadderjobb) and saw that pip freeze didnt show the list of dependencies anymore, just virtualenv. I then removed webdev/envs/wingqvist and created a new one exactly as before (I even restared my computer if that is important). I activated the new (wingqvist) pip installed Django and tried to run python manage.py migrate, and got the same result with ModuleNotFoundError.

I really dont know what to do. I suppose a solution is to pip install all dependencies in (wingqvist) but that sort of goes against the principle of using virtualenvs.

(I updated my python version from 3.6.3 (I think) to 3.6.5 before tampering with virtualenvs, but I dont think that is relevant.)

Any ideas?


Solution

I solved this by completely reinstalling everything that had to do with python and I removed both projects. I also removed every environment variable and restarted from scratch. Then I remade both virtualenvs, created a new django project named wingqvist and git pulled the fadderjobb project. It took a bit of time but everything in (wingqvist) and (fadderjobb) works as expected with the wanted setup.

EDIT:

Now I experience the same problem, but only with the powershell instance I open by writing "powershell" in the address field in explorer when in the project root directory. When I open powershell as administrator by clicking "windows key + x" and clicking windows powershell (administrator) in the list everything works. So the reinstall might have been in vain.



Answered By - Viktor
Answer Checked By - Mary Flores (WPSolving Volunteer)