r/learnpython

2

Posted by5 months ago

= Modules are not getting imported correctly on ubuntu VPS =

![ ](httpswww.redditstatic.com/desktop2x/img/renderTimingPixel.png)

I have the following folder structure

- parent -- corefoo.py -- sourcesbar.py

Inside foo.py

I do this

import os, sys

sys.path.append(os.path.join(os.path.dirname(os.path.abspath(filefrom sources.bar import Bar

And then I execute it like this

python sources/foo.py

I tried this on windows 10 in a virtualenv, it works, and on ubuntu wsl virtualenv it works.

On my ubuntu server in virtualenv too, get this error

from sources.bar import Bar ModuleNotFoundError: No module named 'sources.bar'

I tried executing the script from different pathes, nothing works.

On my ubuntu 20.04 VPS, I have python 3.8.10, on windows I have python 3.9.7, on wsl I have python 3.9.2

Should I upgrade the python on ubuntu to 3.9 or is this an unrelated issue?

67% Upvoted

level 1

os.path.join(os.path.dirname(os.path.abspath(file 

Please allow yourself to enjoy the pleasures of pathlib here,

from pathlib import Path sys.path.append(str(Pathfileresolveparents[1

you might even want to consider using

sys.path.insert(0, str(Pathfileresolveparents[1

to guarantee it will check that folder first and not accidentally pick something from the existing PATH

1

== About Community ==

Subreddit for posting questions and asking for general advice about your python code.

623k

Members

570

Online

Created Oct 2, 2009

Reddit Inc © 2022. All rights reserved