1

I am looking for a solution that will allow me to use py file as a little library.

Short description:

I have a py script and there are a lot of common functions in it. Instead of each time use a big file I want to throw all common functions into separate py file, put into folder Tools and use it when I need.

My problems is that I cannot import this file from Tools, because my script does not see it.

My folder structure:

C:\some\folders\here\my\folder\script.py

C:\some\folders\here\Tools\Library\library.py

Also, it is not good for me to user init.py, because I haven't any python project, it is just one file without any other things.

Are there any normal solutions?

  • Does this answer your question? [How to import a module given the full path?](https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path) – User1010 Mar 07 '21 at 14:10
  • Have you tried to import the .py file inside the file directory? So the two of them, to be in the same directory. – bilakos Mar 07 '21 at 14:21

1 Answers1

1

Python interpreter searches for modules in 3 places:

  • current directory, that means the directory from which you run the script with your import statement
  • list of directories from PYTHONPATH
  • installation-dependent list of directories, this is configures when Python in installed

You can also modify sys.path at runtime and include a directory where your module is located, but this is the worst solution and it's usually discouraged to do so.

Setting PYTHONPATH will most likely be a solution in your situation.

pavelsaman
  • 7,399
  • 1
  • 14
  • 32
  • I got from a different sources that in 'pythonpath' case I need to create init.py –  Mar 07 '21 at 14:33