#๐ im having issues trying to import my own files
141 messages ยท Page 1 of 1 (latest)
@dusky jolt
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.
im using VS if that helps at all
show some example code?
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
i think u might need to do bicycle.test()
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()
this gives me
" AttributeError: module 'bicycle' has no attribute 'test' "
but like i mean
test is a function
so test()
bicycle.test() if the full syntax, i tried to avoid giving it directly
thats whats giving this error
unless i did something else wrong this still isnt working
thats fine and all but that just did not work.... thats why im making this post
Do you have another "bicicle" file in your file tree?
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
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
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
An honest mistake
This article is a bit more in-depth but also has a lot of good examples https://realpython.com/python-import/
@dusky jolt did this aid you or are you still having errors? If so then check out the URL that bread90 provided :)
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
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?
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
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
You can also import the class you desire
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
And import bicycle doesn't work
ive noticed
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
Assuming the blackjack file is the main file
Just make a separate directory for your imports
And do this
Trust me sometime stuff doesn't work, welcome to programming, if the issue persists then take a look at this
something like this?
" No module named 'imports' "
Import
You need __init__.py files in the folders
from imports import bycicle
And I got a very very basic example working, but needed to add the current folder to the sys.path
Add a innit file
Do this ^
im not sure what im doing wrong here lol
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
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()
__init__ needs to be in the imports or main dir
lemme just copy paste this all cuz if this works ima flip
It declares a dir as a package
Imports
i did both to be safe
โ ๏ธ
Do note my top level folder name is different from yours
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.
from cards.imports import bicycle
Imports is not in the cards folder
.
Then they should re-structure the project
Yeah, you need to change the folder name it is importing
.
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
I'm lost at this point xd
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
this should work?
Does it?
no error but no output either
it feels like it should work no
idk what im doing wrong here
It is the folder structure I'm pretty sure
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
am i not able to just have 2 files inteh same folder and import one to the other or do i need all these extra steps
If they are in the same folder it should be easier
thats what i wanted in the begining but that just wouldnt work either
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
started fresh but still this
no error no output
and it opens cmd prompt for half a second
*added (Path after str
You have changed file and folder names?
Also you have deleted the Path( from the sys line
^
How are you running this? It should be from the command line
Can you run it from the command line
i usually just click the play button whats the cmd for running the file
Do you have a command prompt open? And can you navigate to the wooper directory
then you would do py main.py
python main.py?
what does the new imports.py look like
Are all of the files saved?
Yeah that makes things much more complicated to troubleshoot
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
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.