#๐Ÿ”’ im having issues trying to import my own files

141 messages ยท Page 1 of 1 (latest)

vast peakBOT
#

@dusky jolt

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

dusky jolt
#

im using VS if that helps at all

willow lion
#

show some example code?

dusky jolt
#

i put just

def test():
    return "test"
#

and when i run this in another file it spits that error

import bicycle

print(test())
#

theyre both in the same dir aswell if thats helpful

willow lion
#

i think u might need to do bicycle.test()

dusky jolt
#
import bicycle.test()

print(test())
#

its saying now invalid syntax

willow lion
#

import bicycle

#

and then call the. function using the bicycle.test

#

u can also import the test from bicycle so
import test from bicycle
then you can use just test()

dusky jolt
#

but like i mean

willow lion
#

test is a function

#

so test()

#

bicycle.test() if the full syntax, i tried to avoid giving it directly

dusky jolt
dusky jolt
dusky jolt
sinful tusk
dusky jolt
#

nop

sinful tusk
#

Where is your main file

#

And where is your module

dusky jolt
#

does it have to be named main cuz if not BlackJack is where im doing all my stuff atm and module im hoping is bicycle

sinful tusk
#

Yes, I assume your bycicle module isn't materialized from heaven at runtime, maybe you import it from the wrong place? Try

from MODULE_FOLDER import bycicle
pulsar nacelle
#

Looks like there are structure problems with how your files are saved relative to each other, and then there are also problems with your import statements and how you are trying to call them. This article is helpful and covers a lot of general things about importing (linked to the subpackage section here)
https://realpython.com/python-modules-packages/#subpackages

sinful tusk
#

An honest mistake

pulsar nacelle
sinful tusk
#

Module imports can only really mess you up once, then it makes more sense and you tend to never do the same mistake again haha

dusky jolt
#

i just wanted to import my classes for card handling from one file into another that handles the actual blackjack game logic can i not just import file into another file from the same folder or do i have to do all that to make a module?

pulsar nacelle
#

And can you re-state which file you are trying to import bicycle.py from? And what is the exact folder structure? Are all three of those folders at the same level, or are they inside the cards folder

dusky jolt
#

cuz honestly i might just keep the classes in the blackjack file atp ๐Ÿ˜…

#

all are in the same folder and im trying to put bicycle into blackjack

sinful tusk
#

You can also import the class you desire

dusky jolt
#

i have 3 in classes bicycle i want to use all of them but i figured id just test it works w a test func that just prints test but that doesnt seem to be working

sinful tusk
#

And import bicycle doesn't work

dusky jolt
#

ive noticed

sinful tusk
#

Why do you have them all separated like that xd

#

(unrelated)

dusky jolt
#

i wanna make more card games and it made sense to just have a file to make all the cards and shuffle them into a deck(list) and do game logic in each file

sinful tusk
#

Assuming the blackjack file is the main file

#

Just make a separate directory for your imports

sinful tusk
dusky jolt
sinful tusk
#

Yea

#

Does it help?

#

(I also personally find it more manageable in the long run)

dusky jolt
#

" No module named 'imports' "

sinful tusk
#

Import

pulsar nacelle
#

You need __init__.py files in the folders

sinful tusk
pulsar nacelle
#

And I got a very very basic example working, but needed to add the current folder to the sys.path

dusky jolt
#

same error

sinful tusk
#

Add a innit file

dusky jolt
sinful tusk
dusky jolt
#

im not sure what im doing wrong here lol

sinful tusk
#

Me neither xf

#

Xd

dusky jolt
#

yeah like i watched a tut on the subject did as told didnt work looked at stack overflow didnt work and most things ppl have suggested dont work im rlly at a loss

#

i even asked chat gpt and it said the same thing stack overflow and the tut did

pulsar nacelle
#

Here is what I have:

import_test
|
|- __init__.py
|- blackjack.py
|-- imports
    |- bicycle.py
# bicycle.py

def test():
    print("Hello from 'bicycle.py'")
# blackjack.py
import sys
from pathlib import Path

sys.path.append(str(Path(__file__).parent.parent))


from import_test.imports import bicycle

bicycle.test()
dusky jolt
#

__init__ needs to be in the imports or main dir

#

lemme just copy paste this all cuz if this works ima flip

sinful tusk
#

It declares a dir as a package

sinful tusk
dusky jolt
#

i did both to be safe

sinful tusk
#

โ˜ ๏ธ

pulsar nacelle
#

Do note my top level folder name is different from yours

sinful tusk
#

You know what? Make a whole new session open a new folder on your freaking desktop and try to simulate the same situation. See if anything changes.

pulsar nacelle
#

from cards.imports import bicycle

sinful tusk
sinful tusk
pulsar nacelle
#

Then they should re-structure the project

dusky jolt
#

so this did not spit an error

#

but also didnt spit anythign

pulsar nacelle
#

Yeah, you need to change the folder name it is importing

pulsar nacelle
#

And if it is outside the cards folder you may need to do from import_test.imports import bicycle and add an __init__.py in the import_test folder

sinful tusk
#

I'm lost at this point xd

dusky jolt
#

a bit same

#

i just wanted to import one file to another this seems like alot more work for that goal than the youtube guy managed to get working

pulsar nacelle
#

Does it?

dusky jolt
#

no error but no output either

#

it feels like it should work no

#

idk what im doing wrong here

pulsar nacelle
#

It is the folder structure I'm pretty sure

dusky jolt
#

what needs to change

#

and i was under the impression i could import a file from the same folder just fine but this is as close as ive gotten it to work at all so im trusting you lol

dusky jolt
pulsar nacelle
#

If they are in the same folder it should be easier

dusky jolt
#

thats what i wanted in the begining but that just wouldnt work either

pulsar nacelle
#

If they are both in the games folder you should just update blackjack.py to this

# blackjack.py
import sys
from pathlib import Path

sys.path.append(str(Path(__file__).parent.parent))


import bicycle

bicycle.test()
#

My folder structure is like this now:

import_test
|
|- games
    |- blackjack.py
    |- bicycle.py
dusky jolt
#

started fresh but still this

#

no error no output

#

and it opens cmd prompt for half a second

#

*added (Path after str

pulsar nacelle
#

You have changed file and folder names?

dusky jolt
pulsar nacelle
#

Also you have deleted the Path( from the sys line

dusky jolt
pulsar nacelle
#

How are you running this? It should be from the command line

dusky jolt
pulsar nacelle
#

Can you run it from the command line

dusky jolt
#

i usually just click the play button whats the cmd for running the file

pulsar nacelle
#

Do you have a command prompt open? And can you navigate to the wooper directory

#

then you would do py main.py

dusky jolt
willow lion
#

python main.py?

dusky jolt
#

๐Ÿ˜ญ

#

back where we started

willow lion
dusky jolt
#

for all i know this looks like it should work just fine

pulsar nacelle
#

Are all of the files saved?

dusky jolt
#

im gonna fucking scream

#

thank you ๐Ÿ˜ญ

#

ffs

#

an hour cuz the files werent saved

pulsar nacelle
#

Yeah that makes things much more complicated to troubleshoot

dusky jolt
#

and it works without all the pathlib stuff

#

god fucking damn it i had it right the first time i just needed to save the fucking files

#

im going for a smoke this sucked

#

thank you guys for ur help

vast peakBOT
#
Python help channel closed using Discord native close action

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.