#๐Ÿ”’ Issue with OS module and paths/imports

49 messages ยท Page 1 of 1 (latest)

boreal canyonBOT
#

@tawdry bane

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.

cobalt prawn
#

what is your code and do you get an error?

#

your message was deleted
idk why

tawdry bane
#

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

tawdry bane
cobalt prawn
#

if i remember correctly from ..utils.common import * should be from utils.common import *

#

try that, i might be wrong tho

tawdry bane
#

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?

cobalt prawn
#

did you try from utils.common import *?

tawdry bane
#

ModuleNotFoundError: No module named 'utils'

#

relative path is needed

cobalt prawn
tawdry bane
#

No, Im using VSCode "run file" tool on the map.py file

cobalt prawn
tawdry bane
#

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")

cobalt prawn
tawdry bane
#

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

cobalt prawn
#

no, your code is the problem

tawdry bane
#
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

cobalt prawn
tawdry bane
cobalt prawn
#

do you get this output c:/Users/X/main/Library/Programming/Projects/Adventures?

tawdry bane
#

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

cobalt prawn
#

yeah

#

you can send it in dm's

boreal canyonBOT
#
Python help channel closed

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.