Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.
This repository was archived by the owner on Jul 24, 2023. It is now read-only.

Question: how to set the embedded Python to virtualenv Python ? #71

@sindbach

Description

@sindbach

Hi,
I'm trying to write a Go executable that is able to pick up whichever Python version (hence libraries) it's running on. For example, in macOS the default system Python is 2.7.14, but could be executed within Python 3.6.5 virtualenv. As you know, working with virtualenv it's quite nice too, because the libraries installed within the env won't have to be globally installed.

I have tried to prefix the PYTHON_HOME env variable with the VIRTUAL_ENV (Python bin) before python.Initialize(). However this seems to result in still picking the system Python 2.7.14. I can see the PATH env variable started with the correct VIRTUAL_ENV path, but the embedded Python once again is still picking up the system Python.

One of the suggested workarounds on StackOverflow: 7492855 with (Python C API) was to set the program name via Py_SetProgramName. Although I don't seem to be able to find this function on go-python.

Anything suggestions ? or, perhaps I'm missing something quite obvious.

Thanks and regards,
Wan.

Activity

added 2 commits that reference this issue on Aug 6, 2018
10920c6
a69309a
sbinet

sbinet commented on Aug 6, 2018

@sbinet
Owner

#72 exposes Py_SetProgramName and Py_GetProgramName.
could you check this fixes your issue? (please reopen if that's not the case.)

sindbach

sindbach commented on Aug 7, 2018

@sindbach
Author

Hi @sbinet ,
Thanks for the quick patch and reply.

Unfortunately, this doesn't solve the problem (btw, I can't reopen an issue if it's closed by the repo collaborator).

This is the code that I'm using to test:

func init() {
	const py = "/path/venv3/bin/python3"
	python.Py_SetProgramName(py)
	python.Initialize()
	sys := python.PySys_GetObject("version")
	fmt.Println(python.PyString_AsString(sys))
	prefix := python.PySys_GetObject("prefix")
	fmt.Println(python.PyString_AsString(prefix))
}
func main() {
}

For example, if the system Python is 2.7.14. Execute virtualenv -p /path/python3 venv3 to create virtual env with Python3. Once the virtualenv is activated, executing the compiled Go above still returns Python 2.7.14. It should have printed out the correct Python3 version however.

The method above is also what's suggested on docs.python.org/3: Very High Level Embedding.

The only other possibility is Py_SetPythonHome, but I'm not sure whether that will work either.

Any other thoughts or advice ?
Thanks!

reopened this on Aug 7, 2018
sbinet

sbinet commented on Aug 7, 2018

@sbinet
Owner

I can add the API for PythonHome.

but I am quite doubtful this will help: go-python is compiled and linked against a specific CPython API, namely CPython-2.
I'd be surprised compiling against some random CPython v2.x and then running inside a venv with CPython-3 would reliably work.

added a commit that references this issue on Aug 7, 2018
17c9f53
sindbach

sindbach commented on Aug 8, 2018

@sindbach
Author

go-python is compiled and linked against a specific CPython API, namely CPython-2.

Hi,
Again, thanks for the quick patch and also the reply.
You're right, it's linked against specific version of CPython.

Is there a plan on updating the project to be linked against CPython-3 to support Python3 ?
With the hope to just move to Python3 support only.

Thanks!

sbinet

sbinet commented on Aug 8, 2018

@sbinet
Owner

yes, there's a plan.
and there even used to be a PR: #66

I think I'll migrate this repo over to the go-python organization as, say cpy2 and start working on a separate repo, say cpy3, to add CPython-3 support.

(ultimately, the goal is to have a cpy2 or cpy3 low-level package that closely follows the CPython C-API, and a high-level py package which would present a much more Go-idiomatic API.)

sindbach

sindbach commented on Aug 8, 2018

@sindbach
Author

Thanks @sbinet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @sbinet@sindbach

        Issue actions

          Question: how to set the embedded Python to virtualenv Python ? · Issue #71 · sbinet/go-python