3

I've read through many of the other S-O questions relating to this, but still am having trouble getting it to work for me. Apologies in advance for the overlap! I'm using python 2.7.10, on Windows 7.

I'm trying to import a module that I wrote, in my Python Console in PyCharm (doubt that matters). In the console, I navigate until I'm in the directory that contains my module:

/users/usn/.../Tools/my_file.py

which can be confirmed using pwd. I then try

import my_file

but get the error ImportError: No module named my_file. I tried a number of variations to no avail. How can I import the module I wrote, in the console?

Thanks

Amy D
  • 571
  • 2
  • 7
  • 16

5 Answers5

9

You need to extend your environment to the folder where the module is located. Add this to the top of your file into which you are importing your module.

import sys
sys.path.append("/users/usn/.../Tools/")
import my_file
saarrrr
  • 2,754
  • 1
  • 16
  • 26
  • I don't want to permanently add this to my PYTHONPATH. Just importing to run some tests. I should be able to import from directory, no? – Amy D Sep 10 '15 at 19:27
  • 1
    @AmyD this does not alter any environment or path variables, it just extends your path in the file for the life of run. It is environment safe. – saarrrr Sep 10 '15 at 19:29
  • Why do I need to do this if I am already in the same subdirectory? This seemed to fix the issue though--thanks! – Amy D Sep 10 '15 at 19:35
  • 1
    @AmyD navigating to the directory does not alter your path for the module into which you are importing your custom module. This instructs the python runtime to look at the path you appended as well as its default directory when looking for modules to import. You're welcome. – saarrrr Sep 10 '15 at 19:37
3

You can also use imp

import imp
my_file = imp.load_source('name', '/users/usn/.../Tools/my_file.py')

Load and initialize a module implemented as a Python source file and return its module object. If the module was already initialized, it will be initialized again. The name argument is used to create or access a module object. The pathname argument points to the source file. The file argument is the source file, open for reading as text, from the beginning.

Abc Xyz
  • 1,184
  • 12
  • 13
2

To import your module, you need to add its directory to the environment variable, either temporarily or permanently.

Temporarily

import sys
sys.path.append("/path/to/my/modules/")
import my_module

Permanently

Adding the following line to your .bashrc file (in linux) and excecute source ~/.bashrc in the terminal:

export PYTHONPATH="${PYTHONPATH}:/path/to/my/modules/"

Credit/Source: saarrrr, another stackexchange question

Miladiouss
  • 4,270
  • 1
  • 27
  • 34
  • the permanent solution was really useful for me in a dev-container, my lib was importing from itself and every time I was changing something in the code I had to package & install it repetitively ... By exporting this env var I don't have to go through that tedious flow again – Daniel Andrei Mincă Nov 03 '21 at 16:25
0

I believe, one can also use site :

import site
site.addsitedir('/users/usn/.../Tools/')
import my_module

However, why they didn't allow for an import statement to just reference a relative path like in JavaScript escapes me ...

onouv
  • 528
  • 1
  • 5
  • 12
0

You can also take the relative path that you can get for example in codium/vscode when right-clicking on the file or check it out yourself.

You can then directly import with

import relative.path.to.my_file.py
from relative.path.to.my_file.py import my_function

or in the example, likely like:

import some_folder.Tools.my_file.py
questionto42
  • 7,175
  • 4
  • 57
  • 90