Friday 5 April 2019

python - Can I import my own custom modules without using os.chdir()?

I've been following the guide here for creating and importing your own custom modules, but I seem to be missing something.


So far, i have done:



  1. I have the directory C:\Users\Me\OneDrive\CustomModules\MathFunctions.

  2. Added C:\Users\Me\OneDrive\CustomModules to my system environment PATH.

  3. Under MathFunctions, I have 2 files: __init__.py and Functions.py.


After starting Python, simply typing


from MathFunctions import Functions  # Doesn't work

as the guide shows doesn't work.


However, if I use the os.chdir() first, it works:


import os                                       # Works
os.chdir('C:/Users/Me/OneDrive/CustomModules')
from MathFunctions import Functions

Is there some way to avoid using os.chdir()? Or is that a necessary step every time?

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...