Python 2.7, using packages from another location

I've tried searching for this but can't understand the answers that I've seen.

I'm relatively new to Python (2.7) and am the only person at my org who uses it. They're sunsetting one of our virtual environments, and the one which will remain doesn't have any of the necessary third party packages to basically do anything. I need to copy all of the package folders (from 'Python27\Lib\site-packages') to a shared network drive so they're not lost. I've copied the entire Python27 folder over to a shared drive (T:) and want to run them using the copy of Python27 on the new virtual environment (C:)

I want to modify my scripts to import these packages from the networked drive.

The answers I'm seeing for this are similar to:

from application.app.folder.file import func_name

However, this isn't helpful to me, I guess I'm just too stupid to understand it. Lets say I want to import the babel package from the copy I made on T:. The above has me thinking I need to enter something like

from 'T:\foo\Python27\Lib\site-packages' import babel

or

from 'T:\foo\Python2y7\Lib\site-packages\babel' import babel

but every variation of this I try gives syntax errors. Additionally I'm confused, because the examples I see show people pointing to literal '.py' files, but none of these packages have an actual '.py' file of the same name as the package (they all have init files within their individual folders, and lots of other randomly named .py files).

Can someone, in plain english, using filepath examples, show me how to do this? I'm very confused by the long answers and fake example paths, I would love a literal absolute path starting with 'T:\', as answers like

from packA.subA.sa1 import helloWorld

confuse the crap out of me (what is packA? what is subA? is helloWorld the name of the actual package, the name of the folder within site-packages, or the name of a .py file within a folder?) Unfortunately I'm unable to use PIP or any other third party libs that might make this easier, as the new VDE doesn't have anything but stock 2.7 packages.

I'm sorry for being so dumb, I stayed late and created a StackOverflow account just for this, would really appreciate a hand holding.

2 answers

  • answered 2018-11-07 23:52 Prune

    You need to set your PYTHONPATH environment variable to include the path to the folder where application resides. Check that you have a blank or properly coded __init__.py file in each folder in the sequence of inclusion.

  • answered 2018-11-08 00:20 HFBrowning

    The simplest way to do this is:

    import sys
    sys.path.append(r'T:\foo\Python27\Lib\site-packages')
    import babel
    

    Hard-coding a path like this is usually not the best way to do your work, but it's certainly easy to understand and will work in a pinch.