Changing path in Python without leaving virtual environment

  Kiến thức lập trình

I have a program in Python that activates the virtual environment and then returns to project folder and executes the main project file:

os.chdir('venv/Scripts')
os.system('activate')
os.chdir('../../')
os.system('python main.py')

But this code has a problem. It works fine until os.system('activate'), but when I change the path and go back to the project folder to run the file, the program automatically exits the virtual environment that I had activated, and the project doesn’t run properly.

I tried the following code instead of os.chdir but it didn’t work:

os.system('cd ../../')

This code doesn’t even change the path.

LEAVE A COMMENT