I created a python file and used some packages. I installed packages on a virtual Environment. Now, when I try to run that file, it run on the default installed interpreter and I have to activate the virtual Environment every time I have to run that file. Is there any way possible to do that. In conclusion: The code from which the file can select the place where to look for the packages.
You can add the path to the virtual environment's interpreter directly to the shebang at the top of the script. For example, if your virtual environment is stored at /home/ishant/venv
, the shebang would be
#!/home/ishant/venv/bin/python
Then, if you execute your script directly (after making it executable with chmod +x
or the like), your virtual environment will be used.
(Activating a virtual environment simply updates your PATH
variable so that python
resolves to the virtual environment, rather than your "regular" environment. You can always access the tools in the virtual enivironment directly instead.)