0

I want to install updated packages in another directory and have Python grab the updated package instead of the older package.

I'm trying to find a way that I can specify which directory to import for when there are multiple identical packages in sys.path.

I started by running this code to make sure that the path for the second module is present:

import sys
print('\n'.join(sys.path))

Both paths are shown, so I know that Python could find the package from either location.

I run this to see what path Python is using:

import statsmodels
print(statsmodels.__file__)

It is using the path of the out of date version.

I've been looking into using importlib but I haven't figured out how to make that work.

I'm just looking for a way to import a package from a specified path, even when the package exists in another directory in sys.path.

grooveplex
  • 2,492
  • 4
  • 28
  • 30
Jarom
  • 1,067
  • 1
  • 14
  • 36
  • Did you try this solution?: https://stackoverflow.com/questions/4383571/importing-files-from-different-folder – mrbTT Feb 07 '19 at 18:39
  • @mrbTT I did try that solution. I'll admit that I don't understand entirely the technicalities of what it is doing, but I did try to implement it unsuccessfully. – Jarom Feb 07 '19 at 18:46
  • Oh, ok it simply says that when importing stuff it should also consider the mentioned folder. Let's say this. Consider the root folder of your script is `folder_script`. The updated python files you want to import are in a new folder inside `folder_script`(as in `folder_script\folder_i_got_permissions\updated_scripts`) or folder before (as there is `master_folder\folder_script` and the updated are in `master_folder\updated_scripts`) ? – mrbTT Feb 07 '19 at 18:50
  • @mrbTT I got it! I wasn't implementing it correctly because I didn't understand it correctly. Thank you. You can put a solution if you want and I'll accept it. – Jarom Feb 07 '19 at 19:07
  • Okie, added answer explaining the mentioned solution for other people that might land here. – mrbTT Feb 07 '19 at 19:18

1 Answers1

1

as discussed in the commend you'd neeed to implement this solution. With that to further explain what it does, it points to another folder to consider files to import. Considering the mentioned code:

# some_file.py (this is this script you're running)
import sys
sys.path.insert(0, '/path/to/application/app/folder')

import file_name_inside_the_folder_above

You'd let the first argument 0 untouched and just edit the second argument, pointing to which folder the script you have access is. Then you just import as the it's file name.

mrbTT
  • 1,399
  • 1
  • 18
  • 31