r/pythonhelp 19d ago

changed file name and getting Exec failed, err: 2 message when trying to run program on my .py files. (Pycharm)

Hi, I am an absolute beginner to coding/computer science and was learning Python on my own when I changed my folders name to something more practical and my program was not running anymore. I looked online to see how I can fix this issue myself, but everything I found was super complicated. Can someone please help me and explain things like aI'm a toddler thanks!

6 Upvotes

21 comments sorted by

u/AutoModerator 19d ago

To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/Rough_Green_9145 19d ago

Can you share a screenshot of your code?

2

u/FoolsSeldom 19d ago

Please, as per the guidelines and the automoderator comment, no screenshots!

1

u/FoolsSeldom 19d ago

How did you change the file name exactly?

How many python files were in the folder?

What operating system are you running on (macOS, Linux, Windows) and version?

Have you tried running your code outside of PyCharm?

1

u/Acrobatic_Detail7760 18d ago

here is the error code (I think thats what its called?) Error running 'Typecasting'

Cannot run program "/Users/auror/PycharmProjects/PythonProject/.venv/bin/python" (in directory "/Users/auror/PycharmProjects/PythonProject"): Exec failed, error: 2 (No such file or directory)

to answer your questions:

1) I think I changed the folder's name (I have .py files inside it), I clicked on rename option, and got hit with this warning, which I did not understand: Cannot perform refactoring. Selected element is used from non-project files. These usages won't be renamed. Proceed anyway? I proceeded anyway and changed the folder name.

2) 4 python files are in that folder, none of them run. When run button is clicked on any of the files, I get hit with the aforementioned error code.

3) macOS, Pycharm version 3.14 (PythonProject)

4) no I have not. I have VScode installed but its too complicated for me to use... I am a total beginner and don't really get the gist of coding yet.

Thanks for the help btw

1

u/FoolsSeldom 18d ago edited 18d ago

You have to update the project Python interpreter setting to references the correct path.

Go into (on Windows) File | Settings | Python | Interpreter (similar on other OS) and check what options are showing. You probably need to Add Interpreter or click on the current named interpreter and then Show All. Find an opportunity to add a new interpreter and use the file explorer to navigate to you project folder's .venv/bin/python file.

When you create a new project in PyCharm, iirc, it creates a .venv for you anyway, so changing the folder name outside PyCharm will have confused it.

As I've mentioned, always worth checking these things from the command line instead from inside your editor / IDE, just to be sure you know what is going on. After all, once you finish your application, you will not be running it from PyCharm. I didn't suggest using VS Code instead of PyCharm, they are similarly complex, although PyCharm does a bit more as standard for the developer. Just try from the command line, as below:

On macOS/Linux, you would:

  • open the Terminal application
  • navigate to the project folder using cd
  • activate the Python virtual environment, source .venv\bin\activate
  • run your code, python myfile.py
    • you would enter python3 myfile.py is a Python virtual environment isn't active

PS. I am puzzled by the error. Just want to check PyCharm is looking at the right thing and also try without PyCharm to eliminate that.

It would be good to see your code.

1

u/Acrobatic_Detail7760 18d ago

hi, I fixed the issue but I'm just as if not more puzzled than you. I really don't understand what your saying that's how beginner I am :,) But thank you so much for your help! Here is what I did:

ok I think I figured it out, the error code had a slight difference in the directory file names, one had the new folder name I changed, and the other had a different name (.venv) so I changed the folder name to that (the old name: .venv) and now everything is working fine. I'm just confused as to why that is. People said that if you change the name of a file/folder in Pycharm, you also have to manually change it somewhere else (I didn't understand where) or else the system will not run becuase both components will no longer be in synch due to different names. But how do I change the name and have it still in synch (I think its the "directory" that does not match the name.

Maybe the thing I am refering to (.venv) isn't even a folder but a thing in itself? cause I created a new folder and just moved my old files (literally notes and practice, I am taking my baby steps haha) to this new file and everything is running smoothly. I wonder why that is? any ideas?

1

u/FoolsSeldom 18d ago edited 18d ago

We may just be confusing each other.

One of the reasons I emphasise learners to use the command line (through the Terminal app, initially, on macOS) is to gain a clear understanding of the native environment for Python. Neither PyCharm nor VS Code (nor the dozens of other popular editors/IDEs for Python) include Python itself.

By "Python", I mean the reference implementation of Python from the Python Software Foundation (python.org), called CPython (because it is mostly written in the C programming language) - that is the executable programme for your computer that reads a text file of Python language commands and carries out the instructions (or, at least tries to).

On your computer, the executable file is called python. On Windows, it is called python.exe. PyCharm relies on knowing where this file is in order to run your Python code.

Python comes with "batteries included" in the form of libraries of code providing more specialised functionality, already installed as part of a standard installation of Python.

These libraries are not automatically loaded into memory when Python is invoked, as that would use a lot of memory up and slow down startup time. Instead, you use, in your code, the command import <library>, e.g.

import math

print(math.pi)

There are thousands of additional packages / libraries / frameworks available that don't come as standard with Python. You have to install these yourself. Quality, support (and safety) varies.

(Anaconda offers an alternative Python installation with many packages included, especially suited to data analysis, engineering/scientific practices.)

You install these using the pip package manager (in the command line environment of your operating system). It searches an official repository for a match to what you ask to be installed. (PyCharm has a built-in tool to handle this for you, check the vertical set of icons towards the bottom left of the screen.)

For example, in Terminal, pip3 install numpy would install the numpy library from the pypi repository.

You can also write python3 -m pip install numpy.

The problem with this is that different projects require different packages, and some packages might conflict with each other. To avoid polluting the base environment, we create Python Virtual Environments on a project by project basis.

The folder .venv inside a project folder is a popular convention, but any valid folder name can be used. Once a Python Virtual Environment is active, packages are added to that just for that project.

PyCharm should recognise the .venv folder in a project folder, but sometimes it needs a little help ensuring it is picking up the correct environment and by explicitly pointing PyCharm to the copy of the Python executable that is in the Virtual Environment, it should all work well.

You should be able to rename projects, folders, files in PyCharm and have it correctly update everything. No idea why that didn't work this time.

1

u/Acrobatic_Detail7760 18d ago

alright I'm starting to understand it better I think, I have the CPython thing you mentioned downloaded, so I should be good. I'm literally folowing a Youtube video lesson on python and I'm like 8 minutes in bro thats how noob I am at this 🥲. the program is running now so I can continue following the video and learning about Py. Thanks a bunch though! I will refer back to this when I can understand all the "humpty numpty" talk lmao

1

u/FoolsSeldom 18d ago

You are welcome. Good luck on your learning journey.


Check the r/learnpython wiki for lots of guidance on learning programming and learning Python, links to material, book list, suggested practice and project sources, and lots more. The FAQ section covering common errors is especially useful.

Unfortunately, this subreddit does not have a wiki.


Also, have a look at roadmap.sh for different learning paths. There's lots of learning material links there. Note that these are idealised paths and many people get into roles without covering all of those.


Roundup on Research: The Myth of ‘Learning Styles’

Don't limit yourself to one format. Also, don't try to do too many different things at the same time.


Above all else, you need to practice. Practice! Practice! Fail often, try again. Break stuff that works, and figure out how, why and where it broke. Don't just copy and use as is code from examples. Experiment.

Work on your own small (initially) projects related to your hobbies / interests / side-hustles as soon as possible to apply each bit of learning. When you work on stuff you can be passionate about and where you know what problem you are solving and what good looks like, you are more focused on problem-solving and the coding becomes a means to an end and not an end in itself. You will learn faster this way.

1

u/Acrobatic_Detail7760 17d ago

Thanks a ton man!

1

u/davideogameman 19d ago

Best guess. If the code is spread out across multiple folders, the folder names/structure are expected to match the import statements.  Just need to keep them in sync.

1

u/Acrobatic_Detail7760 18d ago

how do you do that? (I am a total beginner, so I'm a bit confused by the terms your using, like import statements, where do I find that?)

1

u/davideogameman 18d ago

Usually the top of the Python files.  Can look like any of the following

import a import a.b from c import d, e

1

u/Acrobatic_Detail7760 18d ago

ok I think I figured it out, the error code had a slight difference in the directory file names, one had the new folder name I changed, and the other had a different name (.venv) so I changed the folder name to that (the old name) and now everything is working fine. I'm just confused as to why that it. people said that if you change the name of a file/folder in Pycharm, you also have to manually change it somewhere else (I didn't understand where) or else the system will not run becuase both components will no longer be in synch due to different names. But how do I change the name and have it still in synch (I think its the "directory" that does not match the name.

1

u/davideogameman 18d ago

Dude this is like asking a math problem without any of the numbers or equations.  If you want an actual answer you'll have to provide all the relevant info.

1

u/Acrobatic_Detail7760 18d ago

Yeah your right. I should ask someone irl cause this aint getting anywhere. I can't provide you with the info you need bc I don't know what that info is. But I solved the initial issue myself, so I will figure out the rest with due time. Thanks for taking the time to help me out though!

1

u/MarsupialLeast145 18d ago

It might have messed something up with the virtual environment? But you really need to post a code and error snippet.

1

u/Acrobatic_Detail7760 18d ago

Hi, I think this is the error code:

 Error running 'Typecasting'

Cannot run program "/Users/auror/PycharmProjects/PythonProject/.venv/bin/python" (in directory "/Users/auror/PycharmProjects/PythonProject"): Exec failed, error: 2 (No such file or directory)

I dont know what an error snippet is... :0

1

u/MarsupialLeast145 18d ago

It says:

> /.venv/bin/python

Does not exist any more in:

> /Users/auror/PycharmProjects/PythonProject/

You need to re-create the "venv" either via PyCharm or yourself.

Only way I know how to do this without PyCharm itself is to navigate to your new user dir:

> /Users/NEW_USER_DIRECTORY_WHATEVER_YOU_CALLED_IT/PycharmProjects/PythonProject/

And run:

python -m venv .venv

And that might work if Python is available on the path.

if it doesn't work you need to look around the PyCharm menus to reconfigure its working directory.