#๐ Issue with OS module and paths/imports
49 messages ยท Page 1 of 1 (latest)
@tawdry bane
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.
hold on, Im working on the formatting
I accidently posted something
\Adventures\map\generator.py๐
from ..utils.common import *
def Generator(dimensions:tuple, bias:int):
returns 0
\Adventures\map\map.py๐
import os
from ..utils.common import random
from .generator import Generator
map = Generator(0,0)
\Adventures\utils\common.py๐
import random
import math
#2 non relevant packages for my entire project
ISSUE
Traceback (most recent call last):
File "c:\Users\x\main\Library\Programming\Projects\Adventures\map\map.py", line 2, in <module>
from ..utils.common import random
ImportError: attempted relative import with no known parent package
DEBUGGING
import os
print("??") #prints ??
print(os.getcwd()) #does NOT print c:/Users/X/main/Library/Programming/Projects/Adventures/map/map.py
os.chdir("C:/") #here im changing the directory to c:/
print(os.getcwd()) #prints c:/
Basically, My problem is that I am trying to reference a file inside my project directory and import some of the modules inside it. Something is wrong with the environment. Im using VSCode
In the debugging, If I change the cwd to c:\ it prints correctly, but not my current working directory
I can simplify this even further, Basically 2 files. One is trying to import from the other but im getting this error
if i remember correctly from ..utils.common import * should be from utils.common import *
try that, i might be wrong tho
map.py is a file inside map folder
which is a folder inside adventures
and the destination can only be reached by referencing adventures
@cobalt prawn any chance I can just upload the code to you?
did you try from utils.common import *?
File "c:\Users\x\main\Library\Programming\Projects\Adventures\main.py", line 1, in <module>
from .utils.common import *
ImportError: attempted relative import with no known parent package
from .utils.common import *
if __name__=="__main__":
print("test")
You'd reach adventure then utils then common.py
Map has no relation here
I dont think the code is the problem, its definetly something in VSCode
I believe I might be inside a restricted workspace or something?
Im not sure how to check this though
no, your code is the problem
import os
print("??") #prints ??
print(os.getcwd()) #does NOT print c:/Users/X/main/Library/Programming/Projects/Adventures/map/map.py
os.chdir("C:/") #here im changing the directory to c:/
print(os.getcwd()) #prints c:/
This is some odd behaviour though
If you want, I can just send you the folder
from wich file are you running this?
do you get this output c:/Users/X/main/Library/Programming/Projects/Adventures?
I did not get this output
it states in the comment that it was not provided, Its blank in the terminal
its meant to be :/Users/X/main/Library/Programming/Projects/Adventures
@cobalt prawn Im really grateful that you took time to try and help me on this. I think it would be easier if I sent you the folder or maybe published it somewhere
Im terrible at explaining the problem
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.