1

I am trying to get a cross platform method for making python files access the a shared module directory in a server hosted file system (Google Drive).
This thread explains how to import modules from other directories pretty well.

And this thread explains how to obtain the user file so that accessing Google Drive, Dropbox, etc. can be made to access the same directory easily, in conjuection with the first idea.

Putting both together:

from os.path import join, expanduser
user_path = expanduser('~')
path_for_pyfiles = join(user_path, 'Google Drive', 'Modules')
print path_for_pyfiles
sys.path.insert(0, path_for_pyfiles)

However, when I run this at a location with a mapped user folder 'U:', and the Google Drive is on 'C:', how do I retrieve the right user path? For clarification, it prints:

'U:\\Google Drive\\Modules'

not

'C:Users\\User\\Google Drive\\Modules'

Summary: How do you get the user path associated with the a given drive?

I tried os.chdir('C:') but that doesn't really affect it.

Community
  • 1
  • 1
chase
  • 3,592
  • 8
  • 37
  • 58

1 Answers1

0

I ended up solving my problem by referring to the environment USERPROFILE instead of relying on the os.path.expanduser() which apparently uses HOME instead (which was my problem - HOME was U:\\ not C:\\ in my case).

So basically this is what was changed:

user_path = environ['USERPROFILE']

And here is what I ended up with:

import sys
from os import path, environ
user_path = environ['USERPROFILE']
path_for_pyfiles = path.join(user_path, 'Google Drive', 'Shared Modules', '')

sys.path.insert(0, path_for_pyfiles)

from shared_module import *

However, I haven,t checked this out on other computers yet, so we'll see if it remains true over time.

If this doesn't solve people's problems I also tried setting the os.environ properties directly.
(It didn't work for me, and I don't know if that's advisable, but if you run into the problem it's somewhere you may look into.)

chase
  • 3,592
  • 8
  • 37
  • 58