GrahamDumpleton / mod_wsgi

Source code for Apache/mod_wsgi.
Apache License 2.0
1.03k stars 268 forks source link

unable to load the file system codec - Windows 2012R2, Apache2.4 and Python 3.7 #535

Closed mcfoi closed 3 years ago

mcfoi commented 4 years ago

I an getting this error: [wsgi:info] [..] mod_wsgi (pid=4444): Python home E:\venvs\meteoapi. [wsgi:info] [..] mod_wsgi (pid=4444): Initializing Python. Fatal Python error: initfsencoding: unable to load the file system codec ModuleNotFoundError: No module named 'encodings'

What I did is the following, while in Windows Server 2012R2:

Install Python 3.7 32bit with symbols Install MS C++ compiler VC14 Install Apache 2.4.41 VC14 32bit in default c:\Apache24 Create virtualenv in E:\venvs\meteoapi Activate virtualenv running E:\venvs\meteoapi\Scripts\activate.bat install mod_wsgi-express with: pip install mod_wsgis This created 'mod_wsgi.cp37-win32.pyd' in e:/venvs/meteoapi/lib/ Run from activated venv mod_wsgi-express module-config Copy output to httpd.conf to load mod_wsgi in Apache. LoadModule wsgi_module "e:/venvs/meteoapi/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win32.pyd" WSGIPythonHome "e:/venvs/meteoapi" Start Apache2.4 with: c:\Apache24\bin\httpd.exe

Apache doesn't start but error.logs shows what reported above:

Fatal Python error: initfsencoding: unable to load the file system codec ModuleNotFoundError: No module named 'encodings'

m4gr4th34 commented 4 years ago

This is the wsgi.py file (located in C:\Users\me\Python_coding_folder\ChatBots\GPT2Local\gpt-2)

`import sys, os import site

site.addsitedir("C:/apienv3/Lib/site-packages")

sys.path.insert(0, 'c:/Users/me/Python_coding_folder/ChatBots/GPT2Local/gpt-2')

from WebAPI import app as application`

Here is the vhosts.conf:

<VirtualHost *:5000> ServerAdmin webmaster@theowner.me.com WSGIScriptAlias / "c:/Users/me/Python_coding_folder/ChatBots/GPT2Local/gpt-2/wsgi.py" DocumentRoot "c:/Users/me/Python_coding_folder/ChatBots/GPT2Local/gpt-2" <Directory "c:/Users/me/Python_coding_folder/ChatBots/GPT2Local/gpt-2"> Require all granted </Directory> ErrorLog "c:/Users/me/Python_coding_folder/ChatBots/GPT2Local/gpt-2/logs/error.log" CustomLog "c:/Users/me/Python_coding_folder/ChatBots/GPT2Local/gpt-2/logs/access.log" common </VirtualHost>

I have python 3.7 and scripts path in the system environments. I even tried adding PYTHONPATH to system environment and user environment, with paths to Python env (c:/apienv3); and path to wsgi.py and webapi.py (my flask app). No combination worked.

For ease of tracking, webapi.py is also located in the folder (C:\Users\me\Python_coding_folder\ChatBots\GPT2Local\gpt-2)

Thanks again for helping out!

GrahamDumpleton commented 4 years ago

That you get the encodings module error for PSF Python indicates you must still be using an older mod_wsgi for that. With the virtual environment activate, ensure you run:

pip uninstall mod_wsgi
pip install --no-cache-dir -U https://github.com/GrahamDumpleton/mod_wsgi/archive/develop.zip

It is very important that you use the --no-cache-dir option else it will not ignore any pre-compiled mod_wsgi you have cached on your system.

m4gr4th34 commented 4 years ago

Thanks. Tried that, uninstall and install, but I still get the same error:

AH00558: httpd.exe: Could not reliably determine the server's fully qualified domain name, using fe80::61e2:230c:abec:6a74. Set the 'ServerName' directive globally to suppress this message [Fri Jul 24 19:29:56.027697 2020] [mpm_winnt:notice] [pid 8660:tid 808] AH00455: Apache/2.4.43 (Win64) mod_wsgi/4.7.1 Python/3.7 configured -- resuming normal operations [Fri Jul 24 19:29:56.028697 2020] [mpm_winnt:notice] [pid 8660:tid 808] AH00456: Apache Lounge VS16 Server built: Apr 21 2020 16:23:13 [Fri Jul 24 19:29:56.028697 2020] [core:notice] [pid 8660:tid 808] AH00094: Command line: 'C:\Apache24\bin\httpd.exe -d C:/Apache24' [Fri Jul 24 19:29:56.031698 2020] [mpm_winnt:notice] [pid 8660:tid 808] AH00418: Parent: Created child process 12280 AH00558: httpd.exe: Could not reliably determine the server's fully qualified domain name, using fe80::61e2:230c:abec:6a74. Set the 'ServerName' directive globally to suppress this message AH00558: httpd.exe: Could not reliably determine the server's fully qualified domain name, using fe80::61e2:230c:abec:6a74. Set the 'ServerName' directive globally to suppress this message [Fri Jul 24 19:29:58.187700 2020] [wsgi:info] [pid 12280:tid 780] mod_wsgi (pid=12280): Initializing Python. Fatal Python error: initfsencoding: unable to load the file system codec ModuleNotFoundError: No module named 'encodings'

Current thread 0x0000331c (most recent call first): [Fri Jul 24 19:29:59.480700 2020] [mpm_winnt:crit] [pid 8660:tid 808] AH00419: master_main: create child process failed. Exiting. [Fri Jul 24 19:29:59.481700 2020] [core:info] [pid 8660:tid 808] AH00096: removed PID file C:/Apache24/logs/httpd.pid (pid=8660)

m4gr4th34 commented 4 years ago

C:\apienv3>.\Scripts\activate

(apienv3) C:\apienv3>pip uninstall mod_wsgi Found existing installation: mod-wsgi 4.7.1 Uninstalling mod-wsgi-4.7.1: Would remove: c:\apienv3\lib\site-packages\mod_wsgi-4.7.1.dist-info* c:\apienv3\lib\site-packages\mod_wsgi* c:\apienv3\scripts\mod_wsgi-express.exe Proceed (y/n)? y Successfully uninstalled mod-wsgi-4.7.1

(apienv3) C:\apienv3>pip install --no-cache-dir -U https://github.com/GrahamDumpleton/mod_wsgi/archive/develop.zip Collecting https://github.com/GrahamDumpleton/mod_wsgi/archive/develop.zip Downloading https://github.com/GrahamDumpleton/mod_wsgi/archive/develop.zip

GrahamDumpleton commented 4 years ago

Does it even start if you comment out:

WSGIPythonHome 'c:/apienv3'

so it isn't used.

m4gr4th34 commented 4 years ago

No, it doesn't start:

LoadFile 'C:/Python/Python37/python37.dll' LoadModule wsgi_module 'c:/apienv3/lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd' WSGIApplicationGroup "%{GLOBAL}"

WSGIPythonHome 'c:/apienv3'

error logs:

AH00558: httpd.exe: Could not reliably determine the server's fully qualified domain name, using fe80::61e2:230c:abec:6a74. Set the 'ServerName' directive globally to suppress this message [Fri Jul 24 19:40:50.170550 2020] [mpm_winnt:notice] [pid 14300:tid 800] AH00455: Apache/2.4.43 (Win64) mod_wsgi/4.7.1 Python/3.7 configured -- resuming normal operations [Fri Jul 24 19:40:50.171550 2020] [mpm_winnt:notice] [pid 14300:tid 800] AH00456: Apache Lounge VS16 Server built: Apr 21 2020 16:23:13 [Fri Jul 24 19:40:50.171550 2020] [core:notice] [pid 14300:tid 800] AH00094: Command line: 'C:\Apache24\bin\httpd.exe -d C:/Apache24' [Fri Jul 24 19:40:50.176550 2020] [mpm_winnt:notice] [pid 14300:tid 800] AH00418: Parent: Created child process 3128 AH00558: httpd.exe: Could not reliably determine the server's fully qualified domain name, using fe80::61e2:230c:abec:6a74. Set the 'ServerName' directive globally to suppress this message AH00558: httpd.exe: Could not reliably determine the server's fully qualified domain name, using fe80::61e2:230c:abec:6a74. Set the 'ServerName' directive globally to suppress this message [Fri Jul 24 19:40:52.353440 2020] [wsgi:info] [pid 3128:tid 740] mod_wsgi (pid=3128): Initializing Python. Fatal Python error: initfsencoding: unable to load the file system codec ModuleNotFoundError: No module named 'encodings'

Current thread 0x00000720 (most recent call first): [Fri Jul 24 19:40:54.043547 2020] [mpm_winnt:crit] [pid 14300:tid 800] AH00419: master_main: create child process failed. Exiting. [Fri Jul 24 19:40:54.043547 2020] [core:info] [pid 14300:tid 800] AH00096: removed PID file C:/Apache24/logs/httpd.pid (pid=14300)

GrahamDumpleton commented 4 years ago

Then the problem is your Python installation and nothing to do with the virtual environment.

Was Python installed for all users on the system?

How are you running Apache, from command line, or as a service?

m4gr4th34 commented 4 years ago

I believe python was run in admin mode, for all users on the system (how can i double check? looking into it again). I'm running Apache as a service.

m4gr4th34 commented 4 years ago

OK! Running from CLI "httpd.exe" in apache24/bin, it's running! un-commenting the 'home' path also removed the numpy stall. I'm getting new errors related to my code which I need to look into. Thanks so much!

m4gr4th34 commented 4 years ago

Thanks Graham, it works great now, all issues resolved! Such a simple thing to overlook. Thanks!

Cito commented 3 years ago

Found this issue because of the error message which I got under Linux using a virtualenv for Python 3. The error obviously happens because Python cannot load its own built-in modules. As Graham already pointed it, this can happen when there's something wrong with the virtualenv. But it can also happen when the virtualenv is ok, but simply is not readable for the Apache process. In my case this was because the virtualenv did not have the proper SELinux context set.

hzuluaga commented 3 years ago

Hello @GrahamDumpleton

I had a similar issue as the one described by @mcfoi ( Fatal Python error: initfsencoding: unable to load the file system codec ModuleNotFoundError: No module named 'encodings' ) on an apache24-windows server 2016 -flask-python3.8.7 64bits - virtualenv deployment .
LoadFile directive was not generated when running "mod_wsgi-express module-config". I then followed your advice to uninstalling mod_wsgi and and instead installing "pip install https://github.com/GrahamDumpleton/mod_wsgi/archive/develop.zip". I get now the LoadFile directive, I copy the returned lines into my httpd.conf file:

LoadFile "c:/program files/python38/python38.dll"
LoadModule wsgi_module "c:/apache24/htdocs/contapermenv/lib/site-packages/mod_wsgi/server/mod_wsgi.cp38-win_amd64.pyd"
WSGIPythonHome "c:/apache24/htdocs/contapermenv"

WSGIScriptAlias / "C:/Apache24/htdocs/contapermenv/wsgi.py"
WSGIPythonPath "C:/Apache24/htdocs/contapermenv/

I run httpd -k start and apache starts ok, no errors in logs file:


Starting the 'Apache2.4' service
The 'Apache2.4' service is running.
pm_winnt:notice] [pid 5496:tid 532] AH00455: Apache/2.4.46 (Win64) mod_wsgi/4.8.0 Python/3.8 configured -- resuming normal operations
[Mon Jan 11 21:52:44.870099 2021] [mpm_winnt:notice] [pid 5496:tid 532] AH00456: Apache Lounge VS16 Server built: Oct  2 2020 11:45:39
[Mon Jan 11 21:52:44.870099 2021] [core:notice] [pid 5496:tid 532] AH00094: Command line: 'C:\\Apache24\\bin\\httpd.exe -d C:/Apache24'
[Mon Jan 11 21:52:44.870099 2021] [mpm_winnt:notice] [pid 5496:tid 532] AH00418: Parent: Created child process 2504
[Mon Jan 11 21:52:45.089511 2021] [mpm_winnt:notice] [pid 2504:tid 560] AH00354: Child: Starting 64 worker threads.
[Mon Jan 11 21:52:52.116409 2021] [wsgi:error] [pid 2504:tid 1072] c:\\apache24\\htdocs\\contapermenv\\Lib\\site-packages\\flask_sqlalchemy\\__init__.py:833: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.\r
[Mon Jan 11 21:52:52.116409 2021] [wsgi:error] [pid 2504:tid 1072]   warnings.warn(FSADeprecationWarning(\r

However when I go to the browser and type "localhost", it keeps loading and loading and my app never shows up!

Screen Shot 2021-01-12 at 11 43 32

the app log does not show any errors at all:

[2021-01-11 20:12:32,260] p736 {C:\Apache24\htdocs\contapermenv\defaultapp\__init__.py:45} DEBUG - Logging is set up.
[2021-01-11 20:12:33,291] p736 {C:\Apache24\htdocs\contapermenv\lib\site-packages\werkzeug\_internal.py:113} INFO -  * Restarting with stat
[2021-01-11 20:12:33,869] p5564 {C:\Apache24\htdocs\contapermenv\defaultapp\__init__.py:45} DEBUG - Logging is set up.
[2021-01-11 20:12:34,244] p5564 {C:\Apache24\htdocs\contapermenv\lib\site-packages\werkzeug\_internal.py:113} WARNING -  * Debugger is active!
[2021-01-11 20:12:34,244] p5564 {C:\Apache24\htdocs\contapermenv\lib\site-packages\werkzeug\_internal.py:113} INFO -  * Debugger PIN: 236-151-857
[2021-01-11 20:12:34,244] p5564 {C:\Apache24\htdocs\contapermenv\lib\site-packages\werkzeug\_internal.py:113} INFO -  * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
[2021-01-11 20:12:46,995] p5564 {C:\Apache24\htdocs\contapermenv\lib\site-packages\werkzeug\_internal.py:113} INFO - 127.0.0.1 - - [11/Jan/2021 20:12:46] "GET / HTTP/1.1" 200 -
[2021-01-11 20:12:47,021] p5564 {C:\Apache24\htdocs\contapermenv\lib\site-packages\werkzeug\_internal.py:113} INFO - 127.0.0.1 - - [11/Jan/2021 20:12:47] "GET /static/css/bootstrap.min.css HTTP/1.1" 200 -
[2021-01-11 20:12:47,232] p5564 {C:\Apache24\htdocs\contapermenv\lib\site-packages\werkzeug\_internal.py:113} INFO - 127.0.0.1 - - [11/Jan/2021 20:12:47] "GET /static/css/main.css HTTP/1.1" 200 -
[2021-01-11 20:12:47,329] p5564 {C:\Apache24\htdocs\contapermenv\lib\site-packages\werkzeug\_internal.py:113} INFO - 127.0.0.1 - - [11/Jan/2021 20:12:47] "GET /static/js/popper.min.js HTTP/1.1" 200 -
[2021-01-11 20:12:47,335] p5564 {C:\Apache24\htdocs\contapermenv\lib\site-packages\werkzeug\_internal.py:113} INFO - 127.0.0.1 - - [11/Jan/2021 20:12:47] "GET /static/js/bootstrap.min.js HTTP/1.1" 200 -
[2021-01-11 20:12:47,346] p5564 {C:\Apache24\htdocs\contapermenv\lib\site-packages\werkzeug\_internal.py:113} INFO - 127.0.0.1 - - [11/Jan/2021 20:12:47] "GET /static/js/jquery-3.5.1.min.js HTTP/1.1" 200 -
[2021-01-11 20:12:47,368] p5564 {C:\Apache24\htdocs\contapermenv\lib\site-packages\werkzeug\_internal.py:113} INFO - 127.0.0.1 - - [11/Jan/2021 20:12:47] "GET /static/js/jquery-ui.min.js HTTP/1.1" 200 -
[2021-01-11 20:12:47,750] p5564 {C:\Apache24\htdocs\contapermenv\lib\site-packages\werkzeug\_internal.py:113} INFO - 127.0.0.1 - - [11/Jan/2021 20:12:47] "GET /favicon.ico HTTP/1.1" 404 -
[2021-01-11 20:12:52,771] p5564 {C:\Apache24\htdocs\contapermenv\lib\site-packages\werkzeug\_internal.py:113} INFO - 127.0.0.1 - - [11/Jan/2021 20:12:52] "GET /login HTTP/1.1" 200 -
[2021-01-11 20:13:36,534] p5564 {C:\Apache24\htdocs\contapermenv\lib\site-packages\werkzeug\_internal.py:113} INFO - 127.0.0.1 - - [11/Jan/2021 20:13:36] "POST /login HTTP/1.1" 200 -
[2021-01-11 21:28:09,508] p4512 {C:/Apache24/htdocs/contapermenv\defaultapp\__init__.py:45} DEBUG - Logging is set up.

What am I missing?

Thanks for any help you could provide

GrahamDumpleton commented 3 years ago

@hzuluaga In future create a new issue, don't ask about new problems on old issues.

Anyway, your problem is that you are starting the Flask debug WSGI server from your WSGI script file and that is never returning as it takes over control of the main loop. You need to guard the running of it as it shouldn't be run under Apache. Eg:

if __name__ == "__main__":
    app.run(...)
hzuluaga commented 3 years ago

I am sorry , I first hesitated at creating a new issue, but thought this behaviour was related to mod_wsgi.

Thanks for your advice, although I do not see where the error is. These are my init.py and wsgi.py files ,respectively:

from defaultapp import create_app

app = create_app()

if __name__ == '__main__':
    app.run(debug=True)
import sys
sys.path.insert(0,"C:/Apache24/htdocs/contapermenv")
from init import app as application

This is my first time deploying flask app on windows & apache. I 've done it over Ubuntu-NGINx and Ubuntu-Apache and it works fine.

Thx again for you help

GrahamDumpleton commented 3 years ago

Well the logs show:

INFO -  * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

which indicates the Flask WSGI server is running.

Can you access that URL?

Only other possibility can think of is that you Apache configuration has something else in it which is resulting in the WSGI script file being loaded as a CGI script.

Trying adding print() statements for debug at the start and end of the script to see if it ever gets to the end of the module when being loaded. Output the value of __name__ from the module.

hzuluaga commented 3 years ago

Sorry I confused you but I made a mistake , those logs were generated launching my app using the command "python init.py" while testing ( no apache behind) , that's why there is a 127.0.0.1:5000 address.

I followed your recommendation and I put "prints" everywhere to see where the loading process of the app was getting stucked , and I found that for some reason the loading failed at a specific library import ...."PANDAS" . I then made a quick search and found Apache has problem when trying to import python libraries such as Pandas, numpy, etc ( https://stackoverflow.com/questions/41099433/flask-hangs-after-importing-pandas-also-numpy-matplotlib-etc ). To solve the issue the following line needs to be included in the httpd.conf file:

WSGIApplicationGroup %{GLOBAL}

It all works perfectly now!!

Thank you very much for your time and help

rdgs

mcilfone commented 3 years ago

Hi, I have the same problem. Apache web server doesn't start correctly, and I have the same issue in log files I'm experiencing the issue on Windows Server 2016 Standard

[Fri Apr 16 15:51:06.467871 2021] [wsgi:info] [pid 1028:tid 120] mod_wsgi (pid=1028): Initializing Python.
Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'

This is my Apache 64 bit configuration

# Enable WSGI
LoadFile "C:/inspiring/Python37/python37.dll"
LoadModule wsgi_module "C:/inspiring/virtualenvs/myvenv/Lib/site-packages/mod_wsgi/server/mod_wsgi.cp37-win_amd64.pyd"
WSGIPythonHome "C:/inspiring/virtualenvs/myvenv/Scripts"

#WSGIApplicationGroup %{GLOBAL}
# Host configuration
WSGIPythonPath "C:/inspiring/virtualenvs/myvenv/Lib/site-packages"
WSGIScriptAlias /       "${ROOT_DIR}/main/main/wsgi.py"
WSGIPassAuthorization On
And this is my wsgi.py, as for the previous responses I've seen in this thread
import os
import sys
import site
from django.core.wsgi import get_wsgi_application
abs_file = os.path.abspath(__file__)
base_app = os.path.dirname(os.path.dirname(abs_file))
# Virtualenv 20.x automatically adds to System Environmental variables a var named VIRTUAL_ENV
# Such var points to the curren active virtual environment, so we can get it to force loading osite-packages
VIRTUAL_ENV = os.getenv("VIRTUAL_ENV")
# add the django project path itself into the sys.path (not the 'django' module)
site.addsitedir(base_app)
# add site-packages from virtualenv, including 'django' and any other requirement
# note that just packages with their own folder set in site-packages are detected. Not simple .py scripts in /site-packages root dir.
site.addsitedir(VIRTUAL_ENV + '/Lib/site-packages')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings')
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

I'm using mod-wsgi 4.61 The particular thing is that on this server I cannot be connected to Internet. So, I copied the modwsgi folder in a virtualenv already built on another PC, on the site-packages folder on this server.

Also I couldn't compile mod-wsgi, even if I download the package with pip on a PC, and then install with pip on the server, getting the downloaded file, becaus I can't install Microsoft VisualStudio Build Tools, that also has a web installer.

Can you help me?

Thanks in advance

Michele Cilfone

GrahamDumpleton commented 3 years ago

@mcilfone In future please create a new issue for your specific problem and not ask against an existing issue.

That said, if you can't use develop branch from GitHub, try just removing WSGIPythonHome.

The value you gave it was wrong, but without develop version WSGIPythonHome doesn't work for other reasons on Windows when using a virtual environment. The value of WSGIPythonPath already uses the workaround for that issue though.

mcilfone commented 3 years ago

@GrahamDumpleton sorry to not change the issue. I thought that my question was strictly related to this issue. I removed the WSGIPythonHome directive, but nothing has changed. I also tried to recreate the virtual environment with the virtualenv package, with no success. Before I created the virtual environment for Python with the command python -m venv. Finally, I removed the virtual environment entirely, using my standard Python installation, but it didn't work again. The strange thing is that, if I launch Apache from the Windows command line, everything goes good. Do you have some suggestions? Thanks in advance.

Edit: I found that, launching Apache from Windows Command Prompt is working good. I tried also to delete and reinstall the Apache service, with no luck.

mcilfone commented 3 years ago

Finally I have Apache running. But I had to change the user for the Apache service from SYSTEM to my current user. Maybe some file that the service couldn't access to?

kwiersma commented 3 years ago

@mcilfone did you install Python system wide? I ran into something like this and discovered I didn't check the right boxes in the Python install to install Python system wide. Once I did that might Apache problems with Python went away.

GrahamDumpleton commented 3 years ago

Closing as believe latest mod_wsgi versions more reliable install/work on Windows. Do not add followups to this issue, instead create a new issue.

shankarnathan commented 2 years ago

The issue is that Apache on Windows, when run as a Service does not pick up the PYTHONHOME environment variable which it needs for the right Python installation to be used.

If it doesn't find it, it gives a very misleading error about the encodings module not found

    Fatal Python error: initfsencoding: unable to load the file system codec
    ModuleNotFoundError: No module named 'encodings'

To get it to work, in a Windows Terminal or CMD, run Apache as a standard executable - not as a Service:

    set PYTHONHOME=<root-of-python-or-conda-env>
    httpd  // In this case, Apache server picks up PYTHONHOME and comes up
    httpd -k start // as a service (need to be admin) - here it DOES NOT pick up PYTHONHOME and fails with the error above

Also note, per the mod_wsgi author, on Windows, Apache seems to ignore the WSGIPythonHome directive, so don't bother going down that rabbit hole.

GrahamDumpleton commented 2 years ago

If Python is installed for all users and not just your own users, am not aware of a problem with it finding the correct Python installation. Also, WSGIPythonHome hopefully shouldn't be an issue with new mod_wsgi versions as a workaround is used for correcting broken behaviour in Python.

So would question whether Python was installed for all users or not.

shankarnathan commented 2 years ago

My Python installations are through conda that comes with the miniconda installation. Not sure exactly what Python Installer does for 'Install for All Users', but the file/folder permissions for the Python installation (through conda) are valid for all users.

Not sure if it matters, but I also use VirtualHosts - so my WSGI configuration is in the httpd-vhosts.conf file.

I tried explicitly running the Apache service with my user credentials, still no dice with mod_wsgi not getting the right Python environment from Apache. Same with Apache's SetEnv and PassEnv.

The only ways I could get mod_wsgi to pick up my Python environment in Windows were:

  1. Set a (global) System environment variable PYTHONHOME and then run Apache as a service or as an exe from the terminal and
  2. Method I described above - set PYTHONHOME in local shell and then run httpd as an exe

I really did not want to do option 1 as it would probably break my entire (conda) virtual Python environments, so I am glad I was able to get mod_wsgi up using option 2.

GrahamDumpleton commented 2 years ago

Anaconda has outright broken the ability for it to be embedded so not surprised it doesn’t work. They just don’t care about that type of usage. I always strongly recommend against using Anaconda Python on Windows if using mod_wsgi. Even on Linux you can have problems with Anaconda Python, but Windows is worse. They have fixed whatever they keep breaking a couple of times in older versions but latest breakage seems to now be more permanent and has been there for a couple of years from memory.

darwinharianto commented 1 year ago

I encountered the same problem, just sharing my solution on it I ended up trying anaconda env and python venv, both didnt work using WSGIPythonHome, So I used shankarnathan solution

in windows terminal (as administrator)

setx PYTHONHOME YOUR_ENV_PATH
setx /m  PYTHONHOME YOUR_ENV_PATH

cd YOUR_APACHE_BIN
httpd -k start

setx PYTHONHOME ""
setx /m  PYTHONHOME ""

reg delete "HKCU%Environment" /v PYTHONHOME /f
reg delete "HKLM%SYSTEM%ControlSet001%Contorl%Session Manager¥Environment" /v PYTHONHOME /f

this way, I can run apache as a service, while not keeping the global environment variable for PYTHONHOME set

GrahamDumpleton commented 1 year ago

@darwinharianto Which may suggest Python was not installed for all users, but only your own user.