2

So this problem is a bit convoluted, but I will try to explain. I have written a script script.py that is located in /my/directory/script.py. This contains many helpful functions that I would like to make available to users of Python on this server. Normally, I would package it as a module and move it to the libs folder and access it this way, but unfortunately at my company I am unable to edit files when they are placed there as I will not have root access.

What I have been doing to access this script from either the shell or other scripts is to write:

import sys
sys.path.append('/my/directory')
from script import *

This works, but ideally I would like it to just be:

import script

What I was thinking was making a folder in libs with __init__.py that contains the import sys code above, and simply referencing this script with an import. But when I do this I get an import error that the script is unable to import from my referenced script.

Is there a better way to do this?

Basically I want people to be able to access my module as if it is installed normally by importing a script that imports my functions.

invoker
  • 507
  • 3
  • 7
  • 18
  • Just tell them to export PATH=$PATH:yourdir and make sure your script has correct permissions. Ideally why not just copy/paste certain versions into libs and then update it when you update your script. It's better that way when you do releases versus constant updates. – Eugene K Sep 17 '15 at 18:00

2 Answers2

-1
import sys
import my.directory.script
sys.modules['script'] = my.directory.script

run this in __init__.py

Malik Tahir
  • 113
  • 7
  • 4
    FYI: This question is 4 years old and the provided answer was flagged for review as a Low Quality Post. Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). This provided answer may be correct, but it could benefit from an explanation. Code only answers are not considered "good" answers. From [review](https://stackoverflow.com/review). – Trenton McKinney Sep 19 '19 at 04:20
-2

You can use pip to install package locally

pip install --user package
dsh
  • 12,037
  • 3
  • 33
  • 51
pacholik
  • 8,607
  • 9
  • 43
  • 55