简体   繁体   中英

Python virtualenv activation working but interpreter doesn't

I've just setup a new environment for my project and uploaded a python repository including bin , lib and project folder. I'm pretty sure I did same previously and it worked without problem. Now when doing the same on an AWS environment I get the error -bash: /projects/scrapy/bin/python2.7: cannot execute binary file . However when doing source /projects/scrapy/bin/activate it successfully activates the environment.

From what I understand, python should be able to execute without any issue no matter the environment ?

Any help or pointing to the right direction would be much appreciated!

python should be able to execute without any issue no matter the environment ?

No, the Python binary is tied to your specific OS and computer architecture. Python source code can usually be run on different machines (provided you didn't use OS-specific features), but that's only made possible by compiling a Python interpreter for the specific target environment first .

In other words, a Python binary compiled to run on macOS will not work on Linux.

All that source bin/activate achieves is that it configures your terminal setting to use the bin directory as the first directory on the PATH search path. This doesn't make bin/python work in another environment, it just means that both environments have a working shell interpreter that can run that script.

Create a new virtualenv with a Python binary compiled for Linux, and install the same packages there. Use Pipenv or a requirements.txt file to transfer the dependencies from Mac to Linux.

For example, using Pipenv you'd copy over the Pipfile and Pipfile.lock files to the other computer, then run pipenv install in the directory there and re-create the virtualenv and dependencies from those files.

I recommend you read up on Python development best practices in the The Hitchhiker's Guide to Python ; this includes such topics on how to manage an environment for a project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM