Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

How can I import another Python file in the same path? [duplicate]

$
0
0

When I was writing a Python package, I hoped my __init__.py to import another Python file in the current folder. It works fine in the local environment, but when I upload it to PyPI and download it, I get the error: ImportError: cannot import module 'run.py'. It confuses me a lot. What should I do now to get rid of this error?
The path of the project:D:\CODES\RunBodies
The file three of the project:
RunBodies
|--__init__.py
|--bdsarg.py
|--setup.py
The code of file__init.py__:

from bdsarg import Bdsarg,Bdargimport numpy as npimport mathclass Body:    def __init__(self, weight, pos0, v0):'''        weight:The weight of the body        pos0:the position of the body,like[2,-4,0]        v0:the velocity of the body,like[3,-1,7]'''        self.m = weight        self.pos = pos0        self.v = v0    def update(self, others, delta_t)->None:        ft = np.array([0, 0, 0])        for bodyi in others:            ft_dir = (bodyi.pos - self.pos) / np.linalg.norm(bodyi.pos - self.pos)            ft = ft + (bodyi.m * self.m / sum(np.square(bodyi.pos - self.pos))) * ft_dir        at = ft / self.m        self.pos = self.pos + self.v * delta_t + 0.5 * at * (delta_t ** 2)        self.v = self.v + at * delta_tclass Bodies:    def __init__(self,bn:int,arg:Bdsarg,tms=0.1):        self.stars=[Body(arg[i][0],arg[i][1],arg[i][2]) for i in range(0,bn)]        self.TIMESTEP = tms    def run(self,t:int)->None:        for _ in np.arange(0,t/self.TIMESTEP):            for body in self.stars:                st = self.stars[:]                st.remove(body)                body.update(st,self.TIMESTEP)    def getpos(self)->list:        lis=[]        for star in self.stars:            lis.append(star.pos)           return lisclass BodiesSR:    def __init__(self,bn:int,arg:Bdsarg,tms=0.1):        li=[]        for v in arg:            li.append((v[0]*math.sqrt(3),                       np.array(v[1]),                       np.array(v[2])))        self.bds=Bodies(bn,li,tms)    def run(self,t:int)->None:        self.bds.run(t)    def getpos(self)->list:        return self.bds.getpos()

The code of filebdsarg.py:

class Bdarg:    def __init__(self,arg:list):        self.arg=arg    def __getitem__(self,i:int):        return self.arg[i]    def __setitem__(self, i:int, v):        self.data[i] = v    def __len__(self):        return len(self.arg)class Bdsarg:    def __init__(self,arg:list[Bdarg]):        self.arg=arg    def __getitem__(self,i:int):        return self.arg[i]    def __setitem__(self, i:int, v):        self.data[i] = v    def __len__(self):        return len(self.arg)

The code of filesetup.py:

from setuptools import setup,find_packagessetup(name='runbds',      version='1.0.0',      description='run the bodies',      author='Galaxy',      author_email='dhjfirst@hotmail.com',      requires= ['numpy'],      packages=find_packages(),        license="apache 3.0"      )

The code to upload the file:
python setup.py sdist
twine upload dist/*

I tried changing the file name, but I still get the bug.


Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>