Suppose there is a Python library called g which is licensed under GPL v2.0 (or v3.0). Suppose further that the library is sufficiently complex that it contains some C/C++ component(s) that are similarly licensed under GPL v2.0.

Now, I'd like to write a program that uses this library:

import g

The import statement here links to the library g, both by loading and compiling g's Python source code, and by dynamically linking to the g's .dll/.so component. According to GPL FAQ, my program has to be licensed under the terms of GPL 2.0 whenever it is propagated:

If a library is released under the GPL (not the LGPL), does that mean that any software which uses it has to be under the GPL or a GPL-compatible license?

Yes, because the program actually links to the library. As such, the terms of the GPL apply to the entire combination. The software modules that link with the library may be under various GPL compatible licenses, but the work as a whole must be licensed under the GPL.

Obviously, publishing a program on Stack Exchange is a form of propagation under GPL's terms, so the question is: can I post this code snippet on SE as a question or answer? After all, the SE Terms of Service require that I provided that code snippet under CC BY-SA license, which is not compatible with GPL:

CC BY-SA 4.0 is one-way compatible with the GNU GPL version 3: this means you may license your modified versions of CC BY-SA 4.0 materials under GNU GPL version 3, but you may not relicense GPL 3 licensed works under CC BY-SA 4.0.

  • 4
    If you are not distributing g via the Web site, I don't see why its license matters. You could just as well have written import some_proprietary_module and this would not stop you in any way from posting the question, since you are not distributing that proprietary module as part of the question.
    – Brandin
    Commented May 18, 2018 at 13:50
  • 2
    "If a library is released under the GPL ... does that mean that any software which uses it ..." - In the context of this FAQ, "uses" means using and copying. GPL (and any license, really) gives you the license to copy the GPL software; copying is what the copyright protects, not "using" in the very broad sense of "here's some code that assumes you have g available on your machine." You can reject the GPL license and still say "import g" and "g.some_api_call_in_that_library()" as much as you like, as long as you don't incorporate g into a program (to do that you'd have to accept the license).
    – Brandin
    Commented May 18, 2018 at 13:58
  • @Brandin As my answer notes, it is not entirely clear whether that would be a derived work or not. Please do not pass on your opinion on this as a fact when this is not a settled matter. Note that under your interpretation of the GPL, the GPL would be equivalent to the LGPL. I agree that this is possible, but I think this is rather unlikely. It seems commonly accepted that non-GPL source code cannot call into GPL libraries.
    – amon
    Commented May 18, 2018 at 14:50
  • 1
    @amon It can only be a derived work if you copied the GPL code in whole or in part into your forum post. The line "import g" is not copying, it is only referring to the fact that the program needs library g to run. In a very loose sense, we might say that the program "uses" the library, but in the sense of the GPL FAQ, nothing is being copied, so it is not really "using" it in any legal sense. The reader of the forum post can copy g on his own (and thus must accept the GPL in doing so), but you don't have to accept g's license just to get the right to put "import g" into your own work.
    – Brandin
    Commented May 18, 2018 at 15:35
  • A summary of most common licensing options: gabrielmoraru.com/software-licenses-are-complicated
    – IceCold
    Commented Mar 17, 2023 at 8:12

2 Answers 2


No, but maybe. You have correctly noted that this is a problem. You cannot paste GPL'ed code to Stack Overflow. But there are two exceptions, which depend on the copyright laws in your local jurisdiction.

  • The code snippet does not fall under copyright. Copyright protects creative expression. If the code snippet is very short, or cannot be creative because there is only one way to write this code, then any license is irrelevant. Where copyrightability starts depends on your jurisdiction.

  • Your usage of this snippet falls under a copyright exception. For example, fair use or a right to quote are common copyright exceptions. Again, this depends on your jurisdiction. However, some kind of right to quote will exist anywhere as it is required by the Berne Convention (a treaty about international copyright).

It is also not 100% clear whether writing code that interfaces with a library creates a derived work of that library. So there is a theoretical possibility that the code snippet is not restricted by the GPL. In practice, you should not rely on this possibility, but assume that the GPL does in fact apply.

In a different scenario, e.g. a blog post that is licensed under CC-BY-SA, you could include the GPL'ed code and specify that it is not available under the CC-BY-SA but the GPL. I.e. the text of the blog post might be CC-BY-SA licensed, but other parts of the post are not. For Stack Overflow, this is not possible because you license the posts in their entirely, and you additionally issue a non-CC-BY-SA fallback license. So in a strict reading of the ToS, you wouldn't even be able to include other CC-BY-SA works in your posts.

Personally, I feel quite comfortable with posting GPL-derived code snippets on Stack Overflow. These will often not be copyrightable because they are both short and not terribly creative. If the post containing such code is discussing the GPL'ed library, and any GPL'ed code snippets are only there to support my statements, and they are attributed properly, then I can likely rely on a right to quote.

  • 1
    Most small snippets on Stack Overflow are for learning, and are of an explanatory nature (they aren't full working programs with the intent of distribution), and would likely fall under fair use and fair dealing.
    – Zizouz212
    Commented May 17, 2018 at 22:04
  • I'm not sure I understand this fully. Suppose the snippet is small and falls under fair use doctrine. The US law says such use is not an infringement of copyright. But copyright is not license. I have the copyright to the code snippet anyways, as I wrote it. So how does fair use help here?
    – Pasha
    Commented May 17, 2018 at 22:53
  • @Pasha if you own copyright to the code snippet, you can post it without problems on SE (as you will be licensing it to SE under its ToS). The fact that the full program is linked to GPL'd g library (to create combined work) is not the issue, as you will not be posting (parts of) g (much less full combined work), but just (parts of) your code (which you can license as you wish). Commented May 18, 2018 at 0:45
  • @Pasha Yes, copyright != license. But you need a license to use someone else's copyrighted work. In this case, we are considering your code snippet which might be a derived work of the GPL library. If the snippet is not copyrightable, you don't need a license. If it is copyrightable and is a derived work, then it contains elements that you authored and elements from the GPL library: you are not the only copyright holder. To publish this derived work, you either need to accept the GPL, or you rely on copyright exceptions such as fair use for your use of the GPL library.
    – amon
    Commented May 18, 2018 at 9:58
  • 2
    @amon there is no derived work when you post source code snippet of your code just because it contains #include <something.h>. That was never actually challenged. But if the person compiles and links that snippet to executable, then produced executable usually is derived work (as it probably includes parts of GPL'd library). Part which is sometimes challenged is that even that executable is not derived work (if using shared instead of static libraries - as executable itself will usually not actually include any library code, but only linker instructions where to find it) Commented May 18, 2018 at 14:37

Yes, you can as it is your code only (and so you can license it to SE as you like). The fact that source code contains text import g, does not make said source code derived work (as it doesn't contain any code that you yourself didn't write), and so license of g is of no importance whatsoever for the purpose of posting said source code snippet.

But if some other person compiles and links that snippet to executable, then produced executable will be derived work (as it includes binary parts of GPL'd library mixed with your binary code), and hence is subject to g's license (which in case of GPL would mean that person could only distribute that executable as GPL, not as CC-BY-SA). Which is what GPL FAQ talks about.

When you use simple interpreter (such as python) instead of compiler, it is even simpler as there is no executable containing mixed code. You can distribute your code under whatever licenses you wish, but you cannot distribute GPL'd library code.

Fair use etc (which might grant you extra rights) does not even need to come into play, as there is no derived work at all when posting snippet of your source code.

To give example for python:

  1. tar zcf blah.tgz your_code/*.py and distribute as CC-BY-SA - OK, it your code only. (The fact that it won't work without library is only usability issue for end user, not legal one for you)
  2. tar zcf blah.tgz gpllib/*.py and distribute as CC-BY-SA - NOT allowed, library is GPL and it is incompatible with CC-BY-SA
  3. tar zcf blah.tgz your_code/*.py gpllib/*.py and distribute as CC-BY-SA - NOT allowed, library is GPL and it is incompatible with CC-BY-SA and so you cannot distribute it as part of something else.

Note however that if you did for example copy/paste part of source code from GPL'd library into your code, it would create derivative work (same as linker would do with combining binary code in binary executable example above), and so it wouldn't be just your code anymore, and you'd fall under point 3. above and be unable to distribute resulting source code as CC-BY-SA.

Another easy way to look into it is to see that import g does not need refer to popular GPL library, it might as well refer to your own reimplementation of the library with same API interface (that you wrote but choose not to publish, for example; or even published it like the LessTif did when it reimplemented Motif back in the day, for example). By looking at the posted source code snippet alone, one cannot even deduce which one it is supposed to be using. Or on SE, your snippet could likely be for example pseudocode, which is never supposed to be able to run and uses some imagined library. The copyright issue (and thus licenses that override it) really only come into play only when you mix code (either source code by hand copying, or binary mixing by the way of linker etc.) from different copyright holder.

  • Interesting... So, suppose I create a new library called h and release it under MIT license. Internally it contains just 1 line: from g import *. By your argument, my library is not a derived work: a derived work is only created when somebody runs a program that uses h. But the user wouldn't even know that, because all they know is that they are using an MIT-licensed library! This can't be right, can it?
    – Pasha
    Commented May 18, 2018 at 17:01
  • 1
    You can, but that MIT library h wont be doing anything unless you also install GPL'd g (or some other g with same subset of API you use). So you'd need to distribute your program+h+g to get functional program (and in doing so would need to follow licenses of all 3 pieces of at the same time, which could only be GPL). For example, Debian has repository Contrib which does something like that (it contains free code, but which won't work without some other non-free prerequisites). So h would be useless if one does not want GPL obligations. Commented May 18, 2018 at 17:16
  • Library g could be already pre-installed in my system. Or some other package could have installed it. Or h may attempt to pip-install g automatically if it fails to import it... I have literally hundreds of python packages on my computer, and for most of them I don't know where they came from, who uses them, and what their license is.
    – Pasha
    Commented May 18, 2018 at 17:37
  • 2
    Take as example posting your C code snippet which has #include <stdio.h> at the top. It could refer to LGPL'd glibc, GPL'd dietlibc, 3BSD Bionic, MIT musl or proprietary Microsoft MSVCRT.DLL C library. Yet the snippet is yours only, and unless you build and distribute binary executable using some of those standard C libraries (or otherwise make combined work) you are not bound by any of their licenses. Commented May 18, 2018 at 17:39
  • 1
    Also note that GPL does not in any way restrict your using of the software, only it's distribution. If you do not distribute the result to others, you're completely free for example to make proprietary changes to GPL library or to use or incorporate parts of GPL code into your closed-source software and use it that way. It's just that you'll never be able to distribute such resulting program to others (and thus limit their freedoms) Commented May 18, 2018 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.