How to use Python version installed with another package as my base Python?

I’m using Windows 10. I have QGIS installed. It comes with Python 3.9. I want to use that as the default Python on my system. Python exists in C:/OSGeo4W64/bin. I was able to use that Python in CMD until yesterday, but then I updated pipenv and now I get an error message (that follows). I uninstalled pipenv and Miniconda. I tried calling “python”, “python3”, and “C:/OSGeo4W64/bin/python.exe” in CMD. Everything I try returns this error message:

C:\Users\japolo>python
Python path configuration:
PYTHONHOME = (not set)
PYTHONPATH = (not set)
program name = ‘python’
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = ‘C:\OSGeo4W64\bin\python.exe’
sys.base_prefix = ‘’
sys.base_exec_prefix = ‘’
sys.platlibdir = ‘lib’
sys.executable = ‘C:\OSGeo4W64\bin\python.exe’
sys.prefix = ‘’
sys.exec_prefix = ‘’
sys.path = [
‘C:\OSGeo4W64\bin\python39.zip’,
‘C:\Users\japolo\Miniconda3\Lib’,
‘C:\Users\japolo\Miniconda3\DLLs’,
‘C:\OSGeo4W64\bin’,
]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named ‘encodings’

Current thread 0x00002660 (most recent call first):

I tried setting PYTHONHOME and PYTHONPATH to C:/OSGeo4W64/bin and unsetting it. Nothing is working. I can’t get Python to work at all. I started QGIS and ran the Python window built in and it works there. I had a separate Python installed a year ago, but it made problems for QGIS, that’s why I’m trying to use the Python in QGIS, so that I don’t install another Python package and have conflicts.

I think I have solved this problem by calling the python EXE that is in C:\OSGeo4W64\apps\Python39. I don’t know why the python in C:\OSGeo4W64\bin doesn’t work while the the python in C:\OSGeo4W64\apps\Python39 works. Since that seems to work, I added
C:\OSGeo4W64\apps\Python39
to my path in environment variables.

Now, I’m trying to find what file or setting creates the “Python path configuration”. That configuration does not reflect the new change in the path and it still includes paths that I’ve deleted, like “‘C:\Users\japolo\Miniconda3\DLLs’,” and I want to edit or reset this configuration, but I can’t tell where it’s saved or how it’s created.

This problem is still around for me. I was able to leave Python alone for a while, but I need it now and I went to install numpy with pip and got the error again. I did a search for the problem and StackOverflow has a question that’s had several 1000 views.

But there is not a good answer. Not even a worthwhile comment. Still looking for help.
There are other questions on StackOverflow that are basically the same. These are the links:

Sorry John, it seems that your question is both hard to solve and (despite the thousands of Stackoverflow views) too niche.

You might try asking the QGIS people, but I fear they will say “don’t touch the QGIS Python!!!”

I think you need a Windows (and cygwin?) expert more than a Python expert, or at least a Python expert who is also a Windows expert, and that rules me out :slight_smile:

Another option might be to go back to your original option:

  • leave the QGIS install alone;
  • reinstall a second Python;
  • if QGIS starts having problems due to the second Python install, tackle those problems.

They may be easier to solve than this problem. Or at least you can push them back on QGIS as their bug to solve. If QGIS insists that they are the only Python on a system, that is surely a bug!

Another option is to ask on other forums. (If you get an answer elsewhere, please come back and document it here too.) Try:

Sorry I can’t be of more help.

1 Like

I am not sure to fully understand your problem, but it looks as if you are trying to use 2 different versions of python on the same computer? If so, it looks like a need for a Python virtual environment.

At least I think you might be able to tweak the problem to be solvable with it.

Here is a link to a howto of mine for doing a venv:
HOWTO - Using Python in a virtual Environment on Linux

1 Like

Hi, Steven. Thanks for pointing out that it’s more of a Windows problem. I had suspected this, but was unsure.

I think it makes sense to not mess with the QGIS install and see if an additional stand-alone Python install causes issues. But I think I’ll try one of the other forums first. If I get useful information somewhere else, I’ll post back to this question.

I appreciate your suggestions.

Ullix, sorry the post is unclear. I installed QGIS, which comes with a Python environment. I figure that instead of installing another Python environment for any time that I need Python outside of QGIS, can I set up my general Windows environment to rely on the Python that is already part of QGIS? This way I don’t install a second Python and then have two Python installations on my computer which could lead to a potential conflict. So I want the Python that is part of QGIS to be my system’s default Python. I tried doing this and that is what leads to the problem that started this thread.

Maybe your suggestion for a virtual environment is a good idea. I can try. Thank you.

What you want to do is possible, but is a confusing setup of environment variables in Windows. I would suggest before you go too much further is to use the OSGE4W Shell and pip to install the libraries you want to use.

  1. Open the shell
  2. Run o4w_env.bat (should set up the environment necessary)
  3. Run python -m pip install numpy
1 Like

Thomas, thanks for your suggestion.

I want to avoid causing some kind of problem, like a conflict, in the OSGEO4W environment.

With that being the case, I saw no option other than remove all Python installs on my system, so I deleted QGIS, since it kind of bricks without the accompanying Python it installs, and start fresh. I still have no idea what was behind that message that I included in OP of this thread, like if it came from QGIS/OSGEO or something in an obscure Windows setting, and without knowing where it came from, figured I should try the fresh start.

I deleted conda and pipenvs and anything on my system related to Python. I went into the registry and tried to clean out any Python settings there as well.

I installed the Python environment available from python.org (v 3.10.x) and that worked fine. Then I installed QGIS again. QGIS installs to its own folder on C: (C:/OSGeo4W64) by default and not in Program Files like the base Python, so the paths for both Pythons are mostly exclusive, which probably helps. I use VS Code to run python and I can use pipenv with it. So far, no problems with Python or QGIS. I’ll mark as solved, hopefully it stays that way. :slight_smile: