0

I have a directory called my.directory and a file in it called file.py, I need to write a python script that uses file.py and I cannot change the names of the files or directories, moreover the script cannot be in the same directory as file.py. I have tried this:

import my.directory.file

and this

import my\.directory.file

neither of which work, I would like to know if this is possible and if so how.

Thanks

edit:

I phrased the above question in a general way because I didn't think the particular details of the problem were important however here are the details anyway. I'm trying to import from python3.6/site-packages

Mathew
  • 1,116
  • 5
  • 27
  • 59
  • Why can’t you change the names of the files or directories? Do you actually need it as a module, or as a script? – Ry- Jan 28 '18 at 21:33
  • I need to import a module that I downloaded, I can't change the names of the directories because there are other scripts dependent on it and I don't want to have to change them all – Mathew Jan 28 '18 at 21:36
  • 1
    If you can't move the module to a place python is aware of, instead make python aware of the location of the module. The easiest way is probably to update your `PYTHONPATH` environment variable. Read more about how how modules are imported [here](https://docs.python.org/3/tutorial/modules.html#the-module-search-path) – Patrick Haugh Jan 28 '18 at 21:41
  • python3.6/site-packages? Is this a venv? – Ry- Jan 28 '18 at 21:56

1 Answers1

0

You cannot have a dot in the module or package name under Python.

But you can execute any arbitrary python code dynamically under your namespace by doing::

>>> exec(open("my.directory.file").read())
Meitham
  • 9,178
  • 5
  • 34
  • 45