Kabocha Porter
Kabocha Porter

Reputation: 301

flask run command fail after folder location change

I changed the location and changed name of my flask folder, which contains a virtual envrionment.

flask run
Fatal error in launcher: Unable to create process using '"c:\previous\path\backend_flask\.venv\scripts\python.exe"  "C:\current\path\flask_backend\.venv\Scripts\flask.exe" run': The system cannot find the file specified.

However,

python -m flask run

still works.

Here is my project structure. .venv folder is a neighbor of app folder.

├── app
│   ├── __init__.py
│   ├── database.py
│   ├── routes.py
│   └── templates
│       └── index.html
├── README.md
├── .gitignore
├── output.txt
└── main.py

How can I fix the error from the flask run command?

Upvotes: 1

Views: 1473

Answers (1)

Kabocha Porter
Kabocha Porter

Reputation: 301

As answered here, there is no clean way to relocate a virtual environment. The flask is installed in the virtual environment and when I run flask run, the virtual environment files cannot find the executables because of the relocation. The same problem occurs if I run pip with virtual environment activated.

Python 3.9

I summaries the solution here, this works regardless whether or not you have relocated. (in my case I am able to freeze with python -m pip freeze even though pip freeze doesn't work).

  1. activate your virtual environment.
  2. python -m pip freeze > requirements.txt to record your current dependencies. Make sure it records the environment in your virtual environment instead of your local environment. (make sure you activate virtual venv before freeze).
  3. deactivate your virtual environment.
  4. remove your current virtual environment folder. rm -r <folder name>
  5. in the new location, install virtual env: python -m venv <name of env>
  6. activate the new virtual env
  7. reinstall the dependencies from the requirement file you just created: python -m pip install -r ./requirements.txt

Upvotes: 1

Related Questions