Hi Maik,

  This is a general Python issue.  Python will only look for files in directories listed in its sys.path variable.  If the current directory "." is not in that path it will not find files in the same directory.  Also if you open a Python file in ChimeraX, ChimeraX itself may be running with a current directory that is not the one containing your Python script file.

  I don't know a very nice way to handle all this.  But an ugly hack would be in myScript.py to check if sys.path contains the directory with your script and if not add it to sys.path.  A more sane Python method is to make a Python module which is a directory containing an __init__.py file and other Python files, and then you use "import mydirectory" and then use relative imports in your Python files like "from . import myclasses".  But that just changes the problem to how do you get ChimeraX to import your Python module which is a directory.

  Hopefully someone else will suggest a nicer way to handle this.

Tom

On Apr 17, 2024, at 2:03 PM, Engeholm, Maik via ChimeraX-users <chimerax-users@cgl.ucsf.edu> wrote:

Hi there,

I'm running a python script either from within ChimeraX (with "open myScript.py") or from the command line (using "chimerax myScript.py"). As the script gets longer and longer, I would like to store some class definitions in a separate file (classDefinitions.py within the same directory as myScript.py) and then import those into the main script with some command like "from classDefinitions import testClass". This, however, gives me an "ModuleNotFoundError: No module named 'testClass'". What is the correct way to do this?

Any help would be much appreciated!

Cheers,

Maik 

_______________________________________________
ChimeraX-users mailing list -- chimerax-users@cgl.ucsf.edu
To unsubscribe send an email to chimerax-users-leave@cgl.ucsf.edu
Archives: https://mail.cgl.ucsf.edu/mailman/archives/list/chimerax-users@cgl.ucsf.edu/