Forums

package basics

I want to have a place on PythonAnywhere that I can import modules from that I've created. These modules will be in different files for ease of debugging, etc. I believe this is called a package, but not sure.

Could you give a little more detail? A package is normally something that's written so that it can be installed from PyPI, which is useful if you want to distribute the code to other people, but not necessarily so important if it's only for your own stuff.

If what you want to do is have a directory on PythonAnywhere where you can put useful stuff that you want to use in different scripts, then the easiest way to do that is to use the PYTHONPATH environment variable, which defines a set of directories (colon-separated) that are searched when you try to import something.

In that case, I want to use PYTHONPATH. I try to use the bash console to do everything although I'm new at that too. Could you give me a basic example of how to do that? I've looked here and online and don't see anything that outlines the very basics and what the commands mean.

THANKS!!

We have a help page for debugging import errors, but the first section covers the theory of how Python finds modules when you do an import. It's here

"For a folder to become a module, it must contain a special file called init.py" I created this file, but it is empty.

I've tried :

import os
import sys
cwd = os.getcwd()
path = cwd + "/my_folder"
if path not in sys.path:
    sys.path.append(path)

This didn't work, and I don't know what to try next.

Can I take a look at your code? We can see it from our side, but we always ask permission first. I just need to know which file you're trying to import things into, and where you're trying to import it from.