0

My project structure is :

$ tree . -L 2
.
├── venv
│   ├── bin
│   ├── include
│   ├── lib
│   ├── lib64 -> lib
│   ├── pip-selfcheck.json
│   └── pyvenv.cfg
├── py -> /path/to/project/venv/bin/python3
├── README.md
├── req.txt
└── source
    └── sym.py

I have a symlink, py pointing to the actual location of the python executable in the venv. This is to ensure that my python executable isn't hardcoded in the scripts, and can be quickly deployed anywhere, while still including a shebang.

Now, when I call the symlink, py vs the actual python executable, /path/to/project/venv/bin/python3, the sys.path value changes. The following is executed in the folder: /path/to/project/source/:

$ ls -l ../py
lrwxrwxrwx. 1 som cms-script-admins 45 Apr 17 19:39 ../py -> /path/to/project/venv/bin/python3

$ cat sym.py
import sys
print(sys.path)


$ ../py sym.py
['/path/to/project/source', '/usr/lib64/python36.zip', '/usr/lib64/python3.6', '/usr/lib64/python3.6/lib-dynload', '/usr/local/lib64/python3.6/site-packages', '/usr/lib64/python3.6/site-packages', '/usr/lib/python3.6/site-packages']

$ /path/to/project/venv/bin/python3 sym.py
['/path/to/project/source', '/usr/lib64/python36.zip', '/usr/lib64/python3.6', '/usr/lib64/python3.6/lib-dynload', '/path/to/project/venv/lib64/python3.6/site-packages', '/path/to/project/venv/lib/python3.6/site-packages']

As you can see, based on whether the sym-link is executed or the direct exec is called, the sys paths are changing.

For example, the symlink includes /usr/lib/python3.6/site-packages but the actual executable in the venv includes /path/to/project/venv/lib/python3.6/site-packages.

How do I fix this?

When I try to import selenium, I get:

$ ../py
>>> import selenium
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'selenium'
3
  • Looks like you created a virtual environment - do you remember doing that? – ajgringo619 Apr 18 at 3:22
  • @ajgringo619 yes, that's intentional. However while running the script, I don't wanna be in the virtual env. Since the python exec is the one in the virtual environment, I still get the same functionality of the venv without actually activating it. – Somenath Sinha Apr 18 at 9:42
  • 1
    I was able to reproduce your situation on my Mint 20 system. I think you might be better off writing a bash wrapper script that sources /path/to/venv/bin/activate before calling python3. – ajgringo619 Apr 19 at 3:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.