Where to put the module?

I downloaded a package from Github. In some directory A, there is a jupyter file which starts with

from  some_module import some_function 

I find that the file ‘some_module.py’ is not in directory A, but in a sibling directory of A.

How did vscode or jupyter know where to look for the module ‘some_module’?

I did not do anything but downloading and unzipping.

ps. I am sorry if this question is very trivial

Hi,

based on your description, you have the following directory set-up (or approximate):

sample_package

One of the ways that Python uses for importing files is by setting up the system PATH. The system PATH includes directories that tells Python where to look when importing modules. It scans them from left-to-right.

If you type the following in your editor, you too can find your system PATHs for a particular module or package:

import sys
sys.path

You can modify your system PATHs at anytime. So, somewhere in the program that you downloaded, the system PATH has been defined.

Example:
For simplicity, let’s assume you have a package with two directories as highlighted by the figure above. directory_A has a.py module and sibling_directory has b.y module.

Let’s assume a.py has the following code:

''' a.py    # C:\Users\mycomp\Desktop\test_pkg\directory_A\a.py  '''

import sys
sys.path.append(r'C:\Users\mycomp\Desktop\test_pkg\sibling_directory')

from b import multi

multi(5, 4)

b.py has the following code:

''' b.py    # C:\Users\mycomp\Desktop\test_pkg\sibling_directory\b.py  '''

def multi(x, y):
    print('x * y is: ', x *y)

Notice that a.py has explicitly told the module where to look for modules when importing (the first two lines of code). If I did not include them, the import would fail. You can try this yourself on your computer. Just make sure the PATH matches your system. Comment out the first two lines and then uncomment them and run again and make observations. Although I have explicitly added this particular PATH in the same module (a.py), somewhere in the package that you downloaded these instructions were added, telling Python (your program) in which directories to look for when importing modules. Note that this is not the only way of setting imports up for your program packages, however.

I hope this helps you with furthering your understanding.

**** UPDATE ****

Here is a nice little trick so that you can use to view the PATH directories a little better:

Type the following to assign a name to the PATH directory list for reference in a for loop:

import sys
path_directories = sys.path 

Next, iterate through the list so that they are printed sequentially:

num = 1
for path in path_directories:
    print('{}. {}'.format(num, path))
    num += 1

Note that the first item in the list may be an empty value ( ’ '), so delete it from the list so that only valid directories are displayed.

1 Like

The mainstream way to install packages these days is with pip, and they go into site-packages, in the library of your your Python interpreter, which is on the standard search path for modules sys.path that @onePythonUser refers to. You can do this from within a Jupyter notebook. Easy install python modules for Jupyter notebooks? - Stack Overflow. There are several alternative commands. I think both answers given here are right, but it depends on your set-up.

pip only works if the project has packaged and published its work on PyPI. (Not quite true: it will install from a package file if you know how.) If you have to download Python packages without that kind of automation, you can put them in site-packages yourself and it should work.

In many cases, you want a site-packages that is private to each project, so you have control over what packages are available where. This is something venv does for you. venv — Creation of virtual environments — Python 3.12.1 documentation . It is well worth learning about.

VSCode or Jupyter may be creating one of these virtual environments behind the scenes. I don’t know enough to say they will/won’t have. If installing packages by hand, I would first ask inside my project where its site-packages was placed.

1 Like