Bad interpreter: No such file or directory but python exists - python-3.x

I know there are a lot of similar questions but I didn't find a proper solution for my problem. Basically I moved some python project from a Mac to another. After moving I change the variable VIRTUAL_ENV in the activate script in order to match the new path. Then I re-create the symlinks of python3 in the virtual environment folder. So the virtual environment should use the python version installed on the Mac (Python 3.9) which work like a charm. Moreover if I run the symlink, it actually use the right python interpreter. If I start the virtual env and then I type
type python3
it seems to use the symlink.
But when I start the project (local instance of Jupyter) it outputs
/***/***/Desktop/DataAnalysis/jupyter-notebook/bin/python3: bad interpreter: No such file or directory
I don't know where is the problem now since python3 points to the right version of python on the Mac

Related

I cannot run pyinstaller on my computer even though I have installed it

This is the problem right here, do you have any advice for that?
installed pip and pyinstaller, but still got this error message when I tried to convert my project into an .exe.
From Pyinstaller installation guide:
If you cannot use the pyinstaller command due to the scripts directory not being in PATH, you can instead invoke the PyInstaller module, by running python -m PyInstaller (pay attention to the module name, which is case sensitive). This form of invocation is also useful when you have PyInstaller installed in multiple python environments, and you cannot be sure from which installation the pyinstaller command will be ran.
So you may run it as e.g.:
python -m PyInstaller some_system.py
Or, as the issue seems that PATH Windows environment variable doesn't include Python's Script folder, it'd better to fix it. From the same guide:
If the command is not found, make sure the execution path includes the proper directory:
Windows: C:\PythonXY\Scripts where XY stands for the major and minor Python version number, for example C:\Python38\Scripts for Python 3.8)
To fix you may run where python to get exact location of Python on your machine (let's say it shows C:\Python38\). Then add to PATH env variable Scripts folder inside it (in this example it'd be C:\Python38\Scripts\)

What is the cause of "Bad Interpreter: No such file or directory"?

I have a Python virtual environment on my linux machine. It has been working fine for two weeks, but all of a sudden I woke up today, and while in the environment I can't execute any commands. For example, if I try to use pip list, or jupyter notebook, this is what I get (env is the name of my environment):
~/env/bin$ pip list
-bash: /home/ubuntu/env/bin/pip: /home/ubuntu/env/bin/python: bad interpreter: No such file or directory
The same thing happens with basically any other command, except Python. Typing python brings up the Python shell just fine. Interestingly it says Anaconda though, when I only used pip with this environment.
I've tried to find info on this but they all seem to be pertaining to running scripts.
Edit: Also want to mention that when I manually look in the environment bin, the packages I installed are all there in green, except Python is in red.
Thank you in advance.
You have a script /home/ubuntu/env/bin/pip and the script has shebang #!/home/ubuntu/env/bin/python but the file is either absent or is not executable.
Check if the file /home/ubuntu/env/bin/python exist. Check if it can be executed by the current user (just run it from the command line). If not — you need to find out a working executable (for example, it could be /home/ubuntu/env/bin/python3), edit the first line of /home/ubuntu/env/bin/pip to fix the shebang.

"venv activate" doesn't not change my Python path

I create a virtual environment; let's say test_venv, and I activate it. All successful.
HOWEVER, the path of the Python Interpreter doesn't not change. I have illustrated the situation below.
For clarification, the python path SHOULD BE ~/Desktop/test_venv/bin/python.
>>> python3 -m venv Desktop/test_venv
>>> source Desktop/test_venv/bin/activate
(test_venv) >>> which python
/usr/bin/python
Please make sure to read Note #2.
This is what you should do if you don't want to create a new virtual environment:
In venv/bin folder there are 3 files that store your venv path explicitly
and if the path is wrong they take the normal python path so you should change the path there to your new path.
change: set -gx VIRTUAL_ENV "what/ever/path/you/need" in activate.fish
change: VIRTUAL_ENV="what/ever/path/you/need" in activate
change: setenv VIRTUAL_ENV "what/ever/path/you/need" in activate.csh
Note #1:
the path is to /venv and not to /venv/bin
Note #2:
If you reached this page it means that you are probably not following Python's best practice for a project structure.
If you were, the process of creating a new virtual environment was just a matter of one command line.
Please consider using one of the following methods:
add a requirements.txt to your project - not recommended.
implement an setup.py script - for real projects.
use a tool like Poetry - just like the latter though somewhat user-friendlier.
Thanks to Khalaimov Dmitrii, I didn't thought it was because I moved the folder.
It is not an answer specifically to your question, but it corresponds the title of the question. I faced similar problem and couldn't find solution on Internet. Maybe someone use my experience.
I created virtual environment for my python project. Some time later my python interpreter also stopped changing after virtual environment activation. Similar to how you described.
My problem was that I moved the project folder to a different directory some time ago. And if I return the folder to its original directory, then everything starts working again.
There is following problem resolution. You save all package requirements (for example, using 'pip freeze' or 'poetry') and remove 'venv'-folder (or in your case 'test_venv'-folder). After that we create virtual environment again, activate it and install all requirements.
This approach resolved my problem.
Check the value of VIRTUAL_ENV in /venv/bin/activate . If you renamed your project directory or moved it, then the value may still be the old value. PyCharm doesn't update your venv files if you used PyCharm to rename the project. You can delete the venv and recreate a new one if the path is wrong, or try the answer that talks about where to change it.

Set the PYTHONPATH for a older version of Python to a directory where I want to install it though I have a default version of Python3

I am using ubuntu 18.04. I have Python-3.6.6 installed as a default version. But for practice I have installed Python-3.6.5 from source code to a directory where I wanted to install it and changed the .bashrc file accordingly. But it is showing me the default python3 path when I call "which" command.
How to use the python version which one I have installed recently?
If you only want to run some practice scripts you can select directly which python you want to use for your script. In the command line, e.g. type
PYTHONPATH=/foo/bar/python365 python somescript.py
where /foo/bar/python365 points to your python-3.6.5
You can also change your PYTHONPATH variable to look first in the python-3.6.5 folder before it looks to the default python (3.6.6) you had installed before. However, I do not recommend this, since it will make all python applications choose the python-3.6.5 as default. For extended practice with python-3.6.5 consider a virtual environment or an anaconda environment.
You should use virtualenv or something similar, like pipenv, I myself use virtualenv.
Here's a guide on how to do that: https://docs.python-guide.org/dev/virtualenvs/
virtualenv is a bit lower level and so not that intuitive to use, but since I used virtualenv before I found pipenv, I just stuck with it, while adding a few aliases to my .bashrc to make it easier.
I used two different python versions because discord.py would break on 3.7 (that doesn't seem to be the case anymore, but I still have it setup with 3.6 and 3.7), so here's what my .bashrc for two different versions looked like, this is on windows, but if you are on unix system or macOS (I want to make this answer as general as possible, it works the same on all operating systems really), just change the paths to lead to your interpreters (whichever you have) and don't use winpty:
alias python36="winpty C:/Users/Hevaesi/AppData/Local/Programs/Python/Python36-32/python.exe"
alias python37="winpty C:/Users/Hevaesi/AppData/Local/Programs/Python/Python37-32/python.exe"
alias pip36="C:/Users/Hevaesi/AppData/Local/Programs/Python/Python36-32/python.exe -m pip"
alias pip37="C:/Users/Hevaesi/AppData/Local/Programs/Python/Python37-32/python.exe -m pip"
alias venv36="python36 -m virtualenv"
alias venv37="python37 -m virtualenv"
alias python="winpty python"
Now, to use Python 3.6, I use venv36 venv in my project folder, and it setups Python 3.6 environment, using venv37 venv would create Python 3.7 environment.
I also wrote a function to make activating less of a hassle (again, this is in bashrc)
activate() {
if [ "$#" == 1 ]; then
source "$1/Scripts/activate"
else
echo "Usage: activate <env_name>"
fi
}
So, after I created my env once, I go to project folder, type activate venv and it starts up whatever version I wanted the project to have all by itself, to run my project I just use python myfile.py, when virtual environment is activated, it will use the interpreter that's in active virtual environment, regardless of what you have in your PATH variable. Also, using pip will automatically install to your virtual environment, not your actual installation, meaning global package list will be clean, or in best case, completely empty.
You should only install what you need and keep it separate environments, normally you use a single environment per project. Well, unless your project is client/server at once (for whatever reason) and server uses different libraries than client, for example, more of them, client environment could have less modules since that's what the server is for, to provide something without having to do it yourself, so they could be two projects in one, hence two different environments, separating dependencies based on what the "unit" in the project really needs.
Note: you have to install virtualenv (or pipenv or whatever you want to use really) into all of your python installations, if you want to create virtual environments from them, that's what pip36 and pip37 here is for - for installing packages directly into actual python installations, for now, I only had to do pip36 install virtualenv and pip37 install virtualenv.

Pycharm and Python Paths on Windows with Conda

I am working with multiple versions of Python, so I don't want to put any one of them in my windows path. I discovered that I can create a virtualenv (under Settings -> Project: X -> Project Interpreter and that will allow me to specify which python exe to use, but also where to get other useful programs, like pip. The end result is that using Virtualenv I can type "pip" or "python" in the terminal in Pycharm and use it as a regular terminal.
But now I'm working with Anaconda so I don't want to use virtualenv and I want to use conda instead. This works fine, but it doesn't seem to put anything into the path. (This is particularly important because where I intend to keep my project is not near my path to the python environment conda created.)
So if I type "pip" or "python" in the pycharm terminal, it doesn't recognize them. I have to specify a huge path to get it to work right.
I've been googling around and trying to find a solution to this problem. I've found a number of supposed solutions, but so far none have worked.
So, for example, I've already tried Add Content Root under project structure. This does nothing. I've tried add local, which seems to just screw everything up. (Presumably this is a way to create a local environment that I shouldn't be touching in conda.) I've tried adding a PathConfig.pth file. So far nothing works.
Remember, I don't want to add anything into the windows path (which obviously would solve my problem) because then it won't know which specific environment I want at the moment.

Resources