0

Trying to import a file (meleeWeapons.py) into my main file (main.py) but it does not seem to be working.

The file directy is as follows

Domination
  |_main.py
  |_meleeWeapons.py
  |_test.py

When I load from Domination import meleeWeapons or from . import meleeWeapons into main.py, trying to load any objects from meleeWeapons into main doesnt work, flagging "myObject" is not defined. When I do the from Domination import meleeWeapons approach, the error "Import "Dominations" could not be resolved"

goose.mp4
  • 272
  • 3
  • 17
  • If your using it in `main.py` does simple `import meleeWeapons` work? – sammy Jun 23 '21 at 23:12
  • @sammy No, it does not – goose.mp4 Jun 23 '21 at 23:13
  • 2
    Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) (MRE). We should be able to copy and paste a contiguous block of your code, execute that file, and reproduce your problem along with tracing output for the problem points. This lets us test our suggestions against your test data and desired output. When I use `import ` in my own structure, it works as expected. – Prune Jun 23 '21 at 23:16

2 Answers2

1

When you import things from a local module, you put the module name first, then the symbols ("objects") second

from meleeWeapons import Domination

If you want to import everything into the global namespace (you rarely, if ever, want to do this), then do this:

from meleeWeapons import *

If you want import the module itself, and using meleeWeapons.Dominion to access Dominion (or any other symbol), then just do a standard import:

import meleeWeapons

You can also give the module an alias:

import meleeWeapons as mW
Samathingamajig
  • 11,839
  • 3
  • 12
  • 34
-1

You're using the wrong syntax. You need

import meleeWeapons

What you did was to tell Python to look in the file Domination.py, and return the symbol meleeWeapons.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • This does not work, but the import doesnt flag errors. Still, no objects are getting imported from `meleeWeapons.py` to `main.py` – goose.mp4 Jun 23 '21 at 23:15
  • @goose.mp4 the objects (functions, variables, classes, etc) would have to be accessed by using `meleeWeapons.OBJECT` – Girardi Jun 23 '21 at 23:21