The answer to what I believe you're asking is "no". I'm assuming you're asking if you can have a web application at
myaccount.pythonanywhere.com which serves particular requests at
/foo and then have DNS configured to somehow redirect
foo.whatever.com to that path. This is not possible purely with DNS configuration, but you may be able to achieve something similar. Allow me to elaborate...
You can set up
foo.whatever.com with a CNAME for
myaccount.pythonanywhere.com in your DNS. You can't include the subdirectory
/foo because DNS only deals with hostnames not paths. In case this isn't clear, bear in mind that a request to, say,
http://foo.whatever.com/path/index.html actually gets split up by your web browser. First it does a DNS lookup for just
foo.whatever.com and what it gets back is an IP address, which in this case will be one owned by PA. Then it connects to this address and makes a HTTP request which starts:
GET /path/index.html HTTP/1.1
[... other HTTP headers here...]
Note that the PA IP address doesn't appear here anywhere - that's just for the browser to know which machine on the Internet to connect to to make the request. The hostname included is that of the original request, which is the important part.
So, at this point you'll get a request coming in to PA with a
Host: header of
foo.whatever.com. At this point the PA front-end receives the request and looks up the hostname against all the registered customer domains - so, you'll need to have a web app configured with the domain
foo.whatever.com, or you'll get the PA "unconfigured domain" error. Note that (AFAIK) it does not look at the path (as far as I know) - it selects a web app based entirely on the domain.
Assuming you have a web app set up, it'll now receive the request and how it's handled is up to your code, modulo the capabilities of whatever framework you're using.
Since each PA web app only allows a single domain to be associated with it, you need to create a new one for each domain. However, if you want to handle multiple domains, I can't see any reason that you can't specify the same code back-end for multiple web apps (i.e. provide the same WSGI python file when configuring multiple domains). Since the WSGI application code has access to the HTTP
Host header (as the
HTTP_HOST environment variable as per the WSGI spec, although most frameworks make this available in a more convenient manner) then there's nothing to stop you using that as well as the path to decide which response to serve. Some frameworks may not make this so convenient, however, as they tend to assume that request routing is based solely on the path and query arguments.
With a bit more work on the WSGI application you could import the same back-end code, and just have the domain-specific WSGI application be a simple wrapper which directs requests based on the paths, which can differ between domains. This is pretty simple - you just have small, unique WSGI applications and use straightforward Python
import to include the common code to do the work. For this to work conveniently, all your WSGI apps would need to be based on the same, or very similar, frameworks.
Finally, you do have a simpler option - set up a very simple WSGI application for
foo.whatever.com that just sends an appropriate
30x response redirecting the user to
myaccount.pythonanywhere.com/foo. Unlike DNS CNAMEs, redirects at the HTTP level can specify a target URL and not just a hostname, but be aware that the address shown in the browser will change with this approach. This may or may not be what you want.
Hopefully I've covered that in enough detail that you're more familiar with what's involved and what your choices are - let me know if you have any more specific questions.